开发者

How to detect more than one keypress simultaneously in C#?

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?

Tha开发者_StackOverflownks


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜