开发者

Trapping a key press

i am using the tu开发者_JS百科rotial here

http://blogs.msdn.com/b/toub/archive/2006/05/03/589423.aspx

This traps a key press and prints it to the console. Does anyone have any idea on how to make this work for control and another key, say ctrl + w? If so could i get some guidance please or could you suggest what i need to research to find out?

Thanks


System.Windows.Forms.Control.ModifierKeys should do the trick, telling you whether shift, alt, and/or control are pressed.

For example;

if (Keys.W == (Keys)vkCode && Keys.Control == Control.ModifierKeys)

should check for ^W combination, according to your link.


If you want to get which Key was pressed then you can use KeyPress event of textbox.

It has KeyPressEventArgs object which contains "KeyChar" property which stores the character of the Key Pressed.

check out this link How do I make a textbox that only accepts numbers?

the problem here is different but the solution provided can help you also .


Just in case the real meta-question here is: how to I implement a hotkey eg Ctrl-W, you can do that using the Win32 RegisterHotKey API.

The example you link to uses a low-level keyboard hook, and while they have their uses, they tend to be rare and specialized (perhaps an automated testing input recording app, or similar). There's also some perf issues with LL keyboard hooks - and note that they can listen to keys, but even if you code reacts to a key, the original key still continues on to the app it was originally destined for. Also, you have to be careful about what you do in your hookproc - don't to anything that could block. Best practice is to just do some basic checks (eg check that it's the key you want), and then post a message to yourself, and then process that message at leisure.

For most general applications, there's usually a better way to do things. If you're thinking of hotkeys, RegisterHotKey is the way to go.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜