无法使用 ::SendInput() 将退格键发送到写字板应用程序

发布于 2024-08-24 12:05:01 字数 1961 浏览 12 评论 0原文

我使用 sendinput() 函数和 Windows 键盘挂钩来开发印度语言的自定义键盘。 项目位于 Google 代码中:http://code.google.com/p/ekalappai

键盘挂钩和 sendinput 函数放置在 win32 dll 中。它们是从 Qt exe 调用的。 我们的应用程序适用于大多数按键和应用程序。我发现以下问题:

我无法将 Backspace 键发送到 Wordpad/Openoffice/MsOffice 等少数应用程序。我发现箭头键和删除键有同样的问题。

这是我的代码:

extern "C" __declspec(dllexport) void GenerateKey(int vk , bool bExtended)
{
    //update previous characters
    previous_2_character = previous_1_character;
    previous_1_character = vk;

    KEYBDINPUT kb={0};
    INPUT Input={0};

    //keydown
    kb.wVk    =  0;
    kb.wScan = vk;/*enter unicode here*/;
    kb.dwFlags = KEYEVENTF_UNICODE; // KEYEVENTF_UNICODE=4
    Input.type = INPUT_KEYBOARD;
    Input.ki = kb;

    ::SendInput(1,&Input,sizeof(Input));

    //keyup
    kb.wVk    =  0;
    kb.wScan = vk;/*enter unicode here*/;
    kb.dwFlags = KEYEVENTF_UNICODE|KEYEVENTF_KEYUP; //KEYEVENTF_UNICODE=4
    Input.type = INPUT_KEYBOARD;
    Input.ki = kb;

    ::SendInput(1,&Input,sizeof(Input));
}

完整的 dll 代码在这里: http://code.google.com/p/ekalappai/source/browse/trunk/ekhook/ekhook/dllmain.cpp

调用代码:

generatekey = (GenerateKey) myLib->resolve( "GenerateKey" );

generatekey(44,FALSE); //comma - THis works in wordpad/MsOffice/Openoffice
generatekey(2949,FALSE); //tamil character "a" works in Wordpad/Msoffice/Openoffice

generatekey(8,FALSE); //backspace - This is NOT working in Wordpad/Msoffice/Openoffice

Qt Exe 的完整调用代码如下: http://code.google.com/p/ ekalappai/source/browse/trunk/ekalappai/window.cpp

我尝试在谷歌中搜索,但还无法找到解决方案。如果有人有解决此问题的线索,请提供帮助。谢谢。

I have used sendinput() function and windows keyboard hooks to develop a custom keyboard for indian languages.
Project is in google code here: http://code.google.com/p/ekalappai

The keyboad hook and sendinput functions are placed in a win32 dll. And they are called from a Qt exe.
Our application works fine for most keys and applications. I find the following issue:

I could not send Backspace key to few applications like Wordpad/Openoffice/MsOffice. I find same issue with Arrowkeys and delete keys.

Here is my code:

extern "C" __declspec(dllexport) void GenerateKey(int vk , bool bExtended)
{
    //update previous characters
    previous_2_character = previous_1_character;
    previous_1_character = vk;

    KEYBDINPUT kb={0};
    INPUT Input={0};

    //keydown
    kb.wVk    =  0;
    kb.wScan = vk;/*enter unicode here*/;
    kb.dwFlags = KEYEVENTF_UNICODE; // KEYEVENTF_UNICODE=4
    Input.type = INPUT_KEYBOARD;
    Input.ki = kb;

    ::SendInput(1,&Input,sizeof(Input));

    //keyup
    kb.wVk    =  0;
    kb.wScan = vk;/*enter unicode here*/;
    kb.dwFlags = KEYEVENTF_UNICODE|KEYEVENTF_KEYUP; //KEYEVENTF_UNICODE=4
    Input.type = INPUT_KEYBOARD;
    Input.ki = kb;

    ::SendInput(1,&Input,sizeof(Input));
}

Full dll code is here: http://code.google.com/p/ekalappai/source/browse/trunk/ekhook/ekhook/dllmain.cpp

Calling code:

generatekey = (GenerateKey) myLib->resolve( "GenerateKey" );

generatekey(44,FALSE); //comma - THis works in wordpad/MsOffice/Openoffice
generatekey(2949,FALSE); //tamil character "a" works in Wordpad/Msoffice/Openoffice

generatekey(8,FALSE); //backspace - This is NOT working in Wordpad/Msoffice/Openoffice

Full calling code from Qt Exe is here:
http://code.google.com/p/ekalappai/source/browse/trunk/ekalappai/window.cpp

I tried searching in google but could not fine a solution yet. If anyone has clue on resolving this pls help. Thanks.

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

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

发布评论

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

评论(2

一百个冬季 2024-08-31 12:05:01

您混淆了虚拟键和扫描码。 wVk成员是重要的成员,只有虚拟键不明确时才会使用扫码。使固定:

kb.wVk   = vk;
kb.wScan = 0;   // TODO: look at VkKeyScanEx()

You are mixing up the virtual key and the scan code. The wVk member is the important one, the scan code will only be used it the virtual key is ambiguous. Fix:

kb.wVk   = vk;
kb.wScan = 0;   // TODO: look at VkKeyScanEx()
始于初秋 2024-08-31 12:05:01

文档说:

如果指定了KEYEVENTF_UNICODE,则SendInput将WM_KEYDOWN或WM_KEYUP消息发送到前台线程的消息队列,wParam等于VK_PACKET。一旦 GetMessage 或 PeekMessage 获取此消息,将消息传递给 TranslateMessage 会发布一条 WM_CHAR 消息,其中包含最初由 wScan 指定的 Unicode 字符。

只是猜测,但文字处理程序可能会触发 WM_KEYDOWN/WM_KEYUP 消息的退格行为,而不是 WM_CHAR。因此,他们可能期望 VK_BACKSPACE (不是 VK_PACKET)作为这些消息的 wParam。它甚至可以通过基于 VKEY 而不是字符的加速器来完成……哎呀,你在 Windows 上,所以几乎一切皆有可能。 :)

您是否尝试过不使用 KEYEVENTF_UNICODE 并执行 kb.wVk = VK_BACKSPACE

(此外,您还可以使用 Spy++ 来更好地了解哪些关键消息发送到目标应用程序以及它与按文字退格键时有何不同。)

The documentation says:

If KEYEVENTF_UNICODE is specified, SendInput sends a WM_KEYDOWN or WM_KEYUP message to the foreground thread's message queue with wParam equal to VK_PACKET. Once GetMessage or PeekMessage obtains this message, passing the message to TranslateMessage posts a WM_CHAR message with the Unicode character originally specified by wScan.

Just a guess, but the word processing programs might be triggering the backspace behavior off of WM_KEYDOWN/WM_KEYUP messages and not WM_CHAR. Thus they may be expecting VK_BACKSPACE (not VK_PACKET) as the wParam of those messages. It might even be done with accelerators based on VKEYs not characters...heck, you're on Windows, so pretty much anything is possible. :)

Have you tried not using KEYEVENTF_UNICODE, and doing kb.wVk = VK_BACKSPACE ?

(Also, you might use Spy++ to get a better clue of what key messages are sent to the target application and how it differs from when you hit a literal backspace.)

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