Hello Guest, please login or register.
Did you miss your activation email?
Login with username, password and session length.

Pages: [1]   Go Down

Author Topic: [C#] Directinput mouse tutorials  (Read 3338 times)

0 Members and 1 Guest are viewing this topic.
[C#] Directinput mouse tutorials
« on: June 20, 2008, 12:42:07 am »
  • =/
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Posts: 2284
 Yeah, anyone know where I can find some... a google search brings up nothing I can use. I am mainly looking how to use the mouse buttons, then the position on the screen btw.
Logged
Re: [C#] Directinput mouse tutorials
« Reply #1 on: June 20, 2008, 12:46:46 am »
  • *
  • Reputation: +3/-0
  • Offline Offline
  • Gender: Male
  • Posts: 6629
Code: [Select]
private Device _keyboardDevice, _mouseDevice;

public void InitializeDevice()
{
// Create the input device's.
_keyboardDevice = new Device(SystemGuid.Keyboard);
if (_keyboardDevice == null) return;
_mouseDevice = new Device(SystemGuid.Mouse);
if (_mouseDevice == null) return;

// Set the cooperative mode of this device.
_keyboardDevice.SetCooperativeLevel(null, CooperativeLevelFlags.Background | CooperativeLevelFlags.NonExclusive);
_mouseDevice.SetCooperativeLevel(null, CooperativeLevelFlags.Background | CooperativeLevelFlags.NonExclusive);

// Decide what data format we want the device to return.
_keyboardDevice.SetDataFormat(DeviceDataFormat.Keyboard);
_mouseDevice.SetDataFormat(DeviceDataFormat.Mouse);

// Aquire a connection to the physical device.
try
{
_keyboardDevice.Acquire();
_mouseDevice.Acquire();
}
catch (DirectXException)
{
_keyboardDevice = null;
_mouseDevice = null;
return;
}
}

You can then get the state of the keyboard using;

Code: [Select]
_keyboardDevice.GetCurrentKeyboardState();

And the mouse state using;

Code: [Select]
_mouseDevice.CurrentMouseState;

Sorry if thats not a very good explanation, I need sleep at the moment >.<.
Logged
Re: [C#] Directinput mouse tutorials
« Reply #2 on: June 20, 2008, 01:16:09 am »
  • =/
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Posts: 2284
 Hmm... I already got that far but thank you for helping. I was more looking for how to check if a mouse button is up or down. : ) Hope you can help in the morning.
Logged
Re: [C#] Directinput mouse tutorials
« Reply #3 on: June 20, 2008, 01:30:50 am »
  • *
  • Reputation: +3/-0
  • Offline Offline
  • Gender: Male
  • Posts: 6629
Hmm... I already got that far but thank you for helping. I was more looking for how to check if a mouse button is up or down. : ) Hope you can help in the morning.

Code: [Select]
byte[] mouseButtons = _mouseState.GetMouseButtons();

If element 1 is set, left mouse button is pressed, if element 2 is set, right mouse button is pressed, if element 3 is set, middle mouse button is pressed.

eg.

Code: [Select]
if (mouseButtons[0] != 0)
      System.Console.WriteLine("Left mouse button is down...");
else if (mouseButtons[1] != 0)
      System.Console.WriteLine("Middle mouse button is down...");
else if (mouseButtons[2] != 0)
      System.Console.WriteLine("Right mouse button is down...");

I really do need sleep now.
Logged
Re: [C#] Directinput mouse tutorials
« Reply #4 on: June 20, 2008, 02:26:59 am »
  • =/
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Posts: 2284
 : D Thank you so much... I hope you sleep well knowing you've helped someone come closer to finishing a game.
Logged
Pages: [1]   Go Up

 


Contact Us | Legal | Advertise Here
2013 © ZFGC, All Rights Reserved



Page created in 0.212 seconds with 44 queries.