views:

80

answers:

2

I am working on a map editor for my game and I use the arrow keys to scroll around the map area. Currently I'm using the KeyDown event, which works fine for scrolling up, down, left or right - but my question would be how to allow diagonal scrolling. Currently, if I hold the right-arrow and then (whilst keeping the right-arrow held) I then press and hold the down-arrow, the down direction replaces the scrolling to the right instead of scrolling diagonally to the bottom right.

Is there a way, for instance, that I could check whether another arrow key is being pressed whilst in the KeyDown event? How can I respond to more than one key being held?

Thanks

+2  A: 

Not easily, the key-down of the 2nd keystroke stops the 1st one from repeating. What you have to do is record that the key is down in the KeyDown event. Cancel that state in the KeyUp event. Next, use a Timer (~250 msec) and in its Tick event check the state of the keys.

Hans Passant
Cheers, I will do it this way.
Matthew Bowen
+1  A: 

The easiest way to do this is to use a Windows API call to get the state of every key on they keyboard, and then check the keys you are interested in: GetKeyboardState via PInvoke.

You can call it on KeyDown/KeyUp, check the state of the keyboard, and act appropriately.

Chris
Thanks, I considered this but I'm keeping platform specific calls to a minimum so that I can keep available the opportunity to port to Mono.
Matthew Bowen