SendInput()以及非英语字符和键盘布局 [英] SendInput() and non-English characters and keyboard layouts

查看:141
本文介绍了SendInput()以及非英语字符和键盘布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Windows中使用非英语输入键盘语言时,我在模拟字符按键时遇到麻烦.

I'm having trouble simulating character keypresses when a non-English input keyboard language is being used in Windows.

我尝试使用KEYEVENTF_UNICODE调用SendInput():

I tried calling SendInput() with KEYEVENTF_UNICODE:

KEYBDINPUT ki;
INPUT input;
int character = 0;

ki.wVk = 0;
ki.wScan = character;
ki.dwFlags = KEYEVENTF_UNICODE;
ki.time = 0;
ki.dwExtraInfo = 0;   
input.type = INPUT_KEYBOARD;
input.ki = ki;
SendInput(1, &input, sizeof(INPUT));

这实际上是可行的(当然,在我的代码中,我也可以在按下键后执行KEYUP)...除了在GTK +应用程序中(可能还有其他实例也不起作用).

And this actually works (of course, in my code, I also do a KEYUP after the key down)... except in GTK+ apps (there may be other instances where it doesn't work either).

根据 MSDN 如果指定了KEYEVENTF_UNICODE,则SendInput将wParam等于VK_PACKET的WM_KEYDOWN或WM_KEYUP消息发送到前台线程的消息队列.一旦GetMessage或PeekMessage获得此消息,则将该消息传递给TranslateMessage会发布WM_CHAR消息,该消息带有wScan最初指定的Unicode字符.如果将该Unicode字符发布到ANSI窗口中,则会自动将其转换为相应的ANSI值.

所以我相信,当传递KEYEVENTF_UNICODE时,SendInput()的功能有所不同……不像通常那样低级.

So I believe that when KEYEVENTF_UNICODE is passed, the functionality of SendInput() is different... not as low-level as it usually is.

在我生命中,我想不出任何其他方法来使SendInput()正确打印出适合用户键盘语言的字符.例如,如果键盘输入语言为瑞典语",则我无法使它输出"@"(而是打印出引号),也无法使其正确输出非ASCII字符,要么(带重音的字母等).

For the life of me, I can't figure out any other way to get SendInput() to print out characters properly for the user's keyboard language. For example, if the keyboard input language is "Swedish", I can't get it to output '@' (instead, it prints out a quotation mark), and I can't get it to output non-ASCII characters properly, either (accented letters, etc).

谢谢.

推荐答案

我发现,执行此操作的正确方法是通过调用字符的VkKeyScanEx()来获取字符的虚拟键代码.返回值的高位字节将告诉您需要按下"哪些修饰键:Control,Alt和/或Shift;低位字节是实际的虚拟键代码.

I discovered that the proper way to do this is to get the virtual-key code of the character by calling VkKeyScanEx() with the character. The high-order bytes of the return value will tell you which modifier keys you need to "press": Control, Alt, and/or Shift; the low-order bytes are the actual virtual-key code.

要获取VK的扫描代码,请致电MapVirtualKeyEx(vkCode, 0);

To get the scan code of the VK, call MapVirtualKeyEx(vkCode, 0);

然后只需要使用刚刚获得的信息进行按键模拟就可以了.

Then it's just a matter of doing the keypress simulation with the information just obtained.

这篇关于SendInput()以及非英语字符和键盘布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆