WinForms,处理所有键= OnKeyPress + OnKeyDown,但是如何呢?
我有一个表单,自定义绘图,没有控件。
当我需要从用户那里获取输入时,我转到 OnKeyDown 事件,然后将事件的 KeyData 映射到字符串** - 这样我就可以识别出用户已按下“ctrl X”等,所以一切都几乎没问题......问题在于不同的键盘设置和语言特定字母 - 所以今天我的用户得到了
ã ->; a
ó -> o
所以我需要 KeyPress 事件,对吗?但是我怎么知道用户输入的是“ł”而不是 alt+L?我现在暂时无法理解这个问题...
** - 我需要这样做,因为我让用户配置快捷方式并使用 .ToString() 生成不太用户友好的文本,例如“OemPeriod,Control” ”或者更好:“ControlKey,Control”
I've got one Form, custom drawing, no controls.
When I need to get the input from the user I go to OnKeyDown event and then map event's KeyData to a string** - so that I can recognize that user has pressed "ctrl X", etc, so everything is almost ok... the problem is with different keyboard settings and language specific letters - so for today my users get
ã -> a
ó -> o
So I need the KeyPress event right? But how do I know that user typed in for example 'ł' and not alt+L? I can't get my head around that for a while now...
** - I need to do it like that since I let users to configure shortcuts and using .ToString() produces not so user friendly text like "OemPeriod, Control" or even better: "ControlKey, Control"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,您需要 KeyPress 事件。或者更确切地说,重写 OnKeyPress。将 KeyDown 事件中的虚拟键代码映射到击键非常困难,您必须了解当前的键盘布局。查看 ToUnicodeEx() 的 MSDN 文档< /a> 看看你面临的是什么。
您不必担心 Alt+L 等组合键。它们不生成 KeyPress 事件。
这是一个例子。启动一个新的 Windows 窗体项目并使代码如下所示:
Yes, you'll need the KeyPress event. Or rather, override OnKeyPress. Mapping virtual key codes in the KeyDown event to key strokes is quite difficult, you'd have to be aware of the current keyboard layout. Take a look at the MSDN docs for ToUnicodeEx() to see what you are up against.
You don't have to worry about keystroke combinations like Alt+L. They don't generate a KeyPress event.
Here's an example. Start a new Windows Forms project and make the code look like this: