无法从另一个线程获取Unicode输入 [英] Unable to get Unicode inputs from another thread
问题描述
嗨
我正在编写一个按键记录器,该按键记录器通过将前台窗口线程与我的线程连接来记录Unicode字符.
当我键入英文字符时,它可以正常工作,并且无论在当前线程中还是在前台线程中都能捕获到正确的大小写(小写或大写).
当我在前景线程中键入阿拉伯字符时,问题是我的当前线程接收到英语对应的字符,因为当当前线程是前景线程时,我的当前线程接收到正确的键盘状态(阿拉伯字符或英语).
这是代码:
Hi
I am writing a key logger which logs Unicode chars by attach foreground window thread with my thread.
It works fine when I type English chars and it catch the right case of chars (small or capital) whether in current thread or in foreground thread .
The problem when I type Arabic chars in foreground thread, my current thread receives the correspond chars in English, as my current thread receives the right keyboard state (Arabic chars or English) when it is the foreground thread.
Here is the code :
LRESULT CALLBACK keyproc(int nCode,WPARAM wparam,LPARAM lparam)
{
if(nCode ==HC_ACTION)
{
if(wparam==WM_KEYUP)
{
KBDLLHOOKSTRUCT *p=(KBDLLHOOKSTRUCT*)lparam;
byte b[256];
wchar_t *buff=new wchar_t[1]; AttachThreadInput(GetWindowThreadProcessId(GetForegroundWindow(),NULL), GetCurrentThreadId(),true);
if (GetKeyboardState(b))
{
ToUnicode(p->vkCode,p->scanCode,b,buff,1,0);
}
s+=QString::fromWCharArray(buff,1);
}
}
return 0;
}
可以帮我吗?
Can one help me ??
推荐答案
看看ToUnicodeEx的文档
http://msdn.microsoft.com/zh-CN /library/windows/desktop/ms646322(v=vs.85).aspx [ http://msdn.microsoft.com/zh-CN /library/windows/desktop/ms646305(v=vs.85).aspx [ http://msdn.microsoft.com/zh-CN /library/windows/desktop/ms646296(v=VS.85).aspx [
Take a look at the documentation for ToUnicodeEx
http://msdn.microsoft.com/en-us/library/windows/desktop/ms646322(v=vs.85).aspx[^]
And for LoadKeyboardLayout
http://msdn.microsoft.com/en-us/library/windows/desktop/ms646305(v=vs.85).aspx[^]
It seems that ToUnicode depends on the current keyboard layout -- which is a process specific thing. You can use ToUnicodeEx to specify a specific keyboard layout.
The trick will be to determine what keyboard layout use.
I looks like GetKeyboardLayout lets you query the active keyboard layout for a specific thread:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms646296(v=VS.85).aspx[^]
Give it a try. See if you can retrieve the active keyboard layout from the foreground process and pass that to ToUnicodeEx.
这篇关于无法从另一个线程获取Unicode输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!