Search code examples
c#.netmultithreadingpollinginput-devices

polling joystick C#


i have an application that records a date/time when the space bar is pressed unfortunately, the window doesn't always have focus, for various reasons. eg virus checker popup, itunes update, windows update, etc

so what i though would be a cool idea is to use a joystick button, then regardless of which window has focus, i can always detect the button press event and record the date/time it was pressed.

now i had a look at a few game tutorials, but they seem to have the joystick polling tied into the screen refresh event, because in a game i guess if the computer is slow and the screen doesn't refresh often, having the joystick button pressed is going to make no difference anyway.

That doesn't really suit my need, so i am looking at running a thread that will poll the joystick every so often

My questions are

  1. is a background thread the best solution
  2. how often should i poll the joystick
  3. what kind of timer should i use to determine the polling frequency.

I would like the responses to be at least equivalent to a keyboard. also need to make sure it doesn't auto-repeat if trigger is held down.

any sample code (C#) appreciated

thanks

alex


Solution

  • Your best bet would be to create a Timer object on your form, and tie the joystick check to the Tick event.

    I've also found that running simple stuff like this every 100ms (10 times a second) has a negligible effect on CPU usage.