在 Compact Framework 中将 Keys.Enter 转换为 char

发布于 2024-09-02 21:18:30 字数 454 浏览 2 评论 0原文

我需要将 Keys.Enter 转换为字符。

这在紧凑框架中不起作用:转换系统序列.Windows.Forms.Keys to a Char (KeyInterop.VirtualKeyFromKey)

有人还有其他想法吗?


进一步澄清: 我更喜欢使用枚举而不是魔法数字/字符。我不想制作自己的 MyKeys.Enter 枚举(等于 '\r\n'),我只想使用 MS 所做的。

Keys.Enter 只是一个示例。


我计划这样做是为了比较 KeyPressEventArgs.KeyChar。这就是为什么我试图与 char 进行比较。

I need to convert Keys.Enter to a char.

This does not work in compact framework: Convert sequence of System.Windows.Forms.Keys to a Char (KeyInterop.VirtualKeyFromKey)

Any one have any other ideas?


Further clarification:
I like using enumerations more than magic numbers/chars. Rather than make my own enumeration that says MyKeys.Enter (that equals '\r\n') I would like to just use what MS has made.

Keys.Enter is just an example.


I am planning to do this to compare the the KeyPressEventArgs.KeyChar. That is why I am trying to compare to a char.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟花易冷人易散 2024-09-09 21:18:33

Keys.Enter 如果在文本字段中输入,则为 "\r\n"

您应该能够通过首先获取 < code>Keys 值,按位与 Keys.KeyCode 进行运算,然后转换为 uint 以获取虚拟键代码。然后将其传递给 MapVirtualKey 将其转换为字符。

Keys.Enter is "\r\n" if typed into a text field.

You should be able to get characters by first taking the Keys value, bitwise and'ing it with Keys.KeyCode, and casting to a uint to get the virtual-key code. Then pass it to MapVirtualKey to convert it to a character.

你的往事 2024-09-09 21:18:32

Keys 枚举中的键是虚拟键。在 Windows 使用当前活动的键盘布局翻译它们之前,它们不会变成字符。根据用户的偏好和语言,具体细节因机器而异。并且很可能是您的软件运行的特定类型的移动设备。此外,许多虚拟键不会产生字符,Keys.F1 就是一个明显的例子。

您应该真正避免自己将虚拟键变成字符,否则您会出错。 Keys.Enter 已经被 Windows 翻译,它将生成 e.KeyChar = '\r' 的 KeyPressed 事件。无论如何,在大多数机器上。

如果你想检测快捷键,那么你可以使用 KeyDown。键入产生字符的按键需要 KeyPressed。

The keys in the Keys enumeration are virtual keys. They don't turn into characters until Windows has translated them, using the currently active keyboard layout. Which is a detail that varies from one machine to the next, depending on the user's preference and language. And quite likely the specific kind of mobile device your software is running on. Also, many virtual keys don't produce a character, Keys.F1 would be an obvious example.

Turning virtual keys into characters yourself is something you should really avoid, you are going to get it wrong. Keys.Enter is already translated by Windows, it will generate the KeyPressed event with e.KeyChar = '\r'. On most machines, anyway.

If you want to detect short-cut keys then you use KeyDown. Typing keys that produce characters require KeyPressed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文