matob

Hi, I want to use the mouse scroll wheel to zoom. In the tutorials I have tried, MouseState.ScrollWheelValue works fine, but in my own test program it always returns 0.

The main difference I can think of in my test program is that I am not using the Game class. Instead I create my own GraphicsDevice and use a standard System.Windows.Form.

Is there any initialization I have to do for MouseState.ScrollWheelValue to work

This is the code for reading mouse state. All of it works, except mouseState.ScrollWheelValue which always returns 0.

MouseState mouseState = Mouse.GetState();
myMouseLocation = new Point(mouseState.X, mouseState.Y);
myIsLeftButtonPressed = mouseState.LeftButton == ButtonState.Pressed;
int currScrollWheelValue = mouseState.ScrollWheelValue;
System.Console.WriteLine("ScrollWheelValue=" + currScrollWheelValue);



Re: XNA Framework MouseState.ScrollWheelValue always 0

matob

... and the Keyboard does not seem to work either.

This code never prints any keys:

System.Console.WriteLine("Pressed keys:");
foreach (Keys k in Keyboard.GetState().GetPressedKeys()) {
System.Console.WriteLine("Key=" + k);
}





Re: XNA Framework MouseState.ScrollWheelValue always 0

Jim.Welch

If you aren't using the Game class, then are the blocks of code ever executing Do you manually call the Update/Draw




Re: XNA Framework MouseState.ScrollWheelValue always 0

matob

The reason the Keyboard.GetState() didn't work was because I called it from a different thread. It seems it only works if it is called from the main thread

I have included a test program. It prints mouse location, scrollWheelValue and pressed keyboard keys in the form title. If variable mUseGameLoopThread is true, we call the Input functions from a separate thread.

Keyboard.GetState() works if mUseGameLoopThread is false. But MouseState.ScrollWheelValue doesn't work. It is always 0.

******************************************

using System.Windows.Forms;
using System.Threading;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

public partial class MainForm : Form {
  protected GraphicsDevice mGraphicsDevice;
  protected Thread mGameLoopThread;
  protected bool mQuitLoop = false;
  protected string mTitle = "";
  protected bool mUseGameLoopThread = true;

  public MainForm() {
    this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);
    InitializeComponent();
    InitGraphicsDevice();
    if (mUseGameLoopThread) {
      StartThread();
    }
  }

  protected void InitGraphicsDevice() {
    PresentationParameters presentationParameters = new PresentationParameters();
    presentationParameters.BackBufferFormat = SurfaceFormat.Bgr32;
    presentationParameters.BackBufferHeight = this.Height;
    presentationParameters.BackBufferWidth = this.Width;
    presentationParameters.SwapEffect = SwapEffect.Discard;
    presentationParameters.IsFullScreen = false;
    presentationParameters.PresentationInterval = PresentInterval.Immediate;
    mGraphicsDevice = new GraphicsDevice(
        GraphicsAdapter.DefaultAdapter, DeviceType.Hardware, this.Handle,
        CreateOptions.HardwareVertexProcessing, presentationParameters);
  }

  protected void UpdateInput() {
    string keys = "";
    foreach (Microsoft.Xna.Framework.Input.Keys k in Keyboard.GetState().GetPressedKeys()) {
      keys += k;
    }
    MouseState mouseState = Mouse.GetState();
    mTitle = string.Format("MouseLoc=({0},{1}), MouseScroll={2}, Keys={3}",
        mouseState.X, mouseState.Y, mouseState.ScrollWheelValue, keys);
  }

  protected override void OnPaint(PaintEventArgs e) {
    mGraphicsDevice.Clear(Color.CornflowerBlue);
    mGraphicsDevice.Present();
    if (!mUseGameLoopThread) {
      UpdateInput();
    }
    this.Text = mTitle;
    this.Invalidate();
  }

  protected void StartThread() {
    mGameLoopThread = new Thread(new ThreadStart(RunGameLoopThread));
    mGameLoopThread.Start();
  }

  protected void RunGameLoopThread() {
    while (!mQuitLoop) {
      UpdateInput();
      Thread.Sleep(50);
    }
  }

  private void MainForm_FormClosing(object sender, FormClosingEventArgs e) {
    mQuitLoop = true;
  }
}





Re: XNA Framework MouseState.ScrollWheelValue always 0

Shawn Hargreaves - MSFT

Yes, on Windows input only works from the same thread that owns the game window.

To read mouse input from a custom window you need to register your window handle with the Mouse object, using the WindowHandle property.





Re: XNA Framework MouseState.ScrollWheelValue always 0

Milow

I'm unable to get the ScrollWheelValue to change from 0 whether I'm on the ui thread or not. I'm currently reading keyboard input on the UI thread and mouse position on a multimedia timer thread.

I have put the following code on the same thread that successfully captures keyboard input:

MouseState mouseState = Mouse.GetState();
if(mouseState.ScrollWheelValue != 0)
{
Console.Out.WriteLine("ScrollWheelValue = {0}", mouseState.ScrollWheelValue);
}

The value is never output when I move the scroll wheel. I've even tried disabling all calls to Mouse.GetState() on the mouse position thread in case it interferes, but no success.

I should add that everything else is being captured perfectly. Keyboard, mouse move and mouse button events.

Any ideas




Re: XNA Framework MouseState.ScrollWheelValue always 0

Shawn Hargreaves - MSFT

You need to set Mouse.WindowHandle to get this to work.





Re: XNA Framework MouseState.ScrollWheelValue always 0

Milow

Thanks Shawn. It worked. I had tried this earlier and got some strange behavior. I can only guess I somehow got a handle to the wrong window. I ended up using:

MouseWindowHandle = m_RenderControl.Handle;

where m_RenderControl is the PictureBox that XNA is rendering to.




Re: XNA Framework MouseState.ScrollWheelValue always 0

Milow

Actually had to change that to effectiely:

Mouse.WindowHandle = m_RenderControl.FindForm().Handle;