为什么PostMessage在发送小写键时会发送多个键?
Why does PostMessage send multiple keys when sending lower case keys?
我正在使用 PostMessage
将密钥发送到最小化的 firefox 实例。下面的代码适用于大写字符,但是当我发送小写字符时,它似乎发送了每个发送的字符中的 3 个。
这是发送 "c" 字符的简化示例。我知道它不是 firefox,因为它在记事本中也是如此。
这是我手动将输入发送到 window
时的 spy++
输出
<000001> 000204B6 P WM_KEYDOWN nVirtKey:'C' cRepeat:1 ScanCode:2E fExtended:0 fAltDown:0 fRepeat:0 fUp:0
<000002> 000204B6 P WM_CHAR chCharCode:'99' (99) cRepeat:1 ScanCode:2E fExtended:0 fAltDown:0 fRepeat:0 fUp:0
<000003> 000204B6 P WM_KEYUP nVirtKey:'C' cRepeat:1 ScanCode:2E fExtended:0 fAltDown:0 fRepeat:1 fUp:1
这是我的代码发送时的spy++
输出
<000001> 000906D4 P WM_KEYDOWN nVirtKey:'C' cRepeat:0 ScanCode:00 fExtended:0 fAltDown:0 fRepeat:0 fUp:0
<000002> 000906D4 P WM_CHAR chCharCode:'99' (99) cRepeat:0 ScanCode:00 fExtended:0 fAltDown:0 fRepeat:0 fUp:0
<000003> 000906D4 P WM_KEYUP nVirtKey:'C' cRepeat:0 ScanCode:00 fExtended:0 fAltDown:0 fRepeat:0 fUp:0
<000004> 000906D4 P WM_CHAR chCharCode:'99' (99) cRepeat:0 ScanCode:00 fExtended:0 fAltDown:0 fRepeat:0 fUp:0
<000005> 000906D4 P WM_CHAR chCharCode:'99' (99) cRepeat:0 ScanCode:00 fExtended:0 fAltDown:0 fRepeat:0 fUp:0
我在这里做错了什么?
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool PostMessage(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);
const int WM_KEYDOWN = 0x100;
const int WM_KEYUP = 0x101;
const Int32 WM_CHAR = 0x0102;
IntPtr val = new IntPtr((Int32)'c');
PostMessage(WindowHandle, WM_KEYDOWN, (IntPtr)(val - 0x020), new IntPtr(0));
PostMessage(WindowHandle, WM_CHAR, (IntPtr)val, new IntPtr(0));
PostMessage(WindowHandle, WM_KEYUP, (IntPtr)(val - 0x020), new IntPtr(0));
想通了。我只需要以下内容!
char Letter = 'a';
PostMessage(WindowHandle, WM_CHAR, (IntPtr)Letter, new IntPtr(0));
我正在使用 PostMessage
将密钥发送到最小化的 firefox 实例。下面的代码适用于大写字符,但是当我发送小写字符时,它似乎发送了每个发送的字符中的 3 个。
这是发送 "c" 字符的简化示例。我知道它不是 firefox,因为它在记事本中也是如此。
这是我手动将输入发送到 window
时的spy++
输出
<000001> 000204B6 P WM_KEYDOWN nVirtKey:'C' cRepeat:1 ScanCode:2E fExtended:0 fAltDown:0 fRepeat:0 fUp:0
<000002> 000204B6 P WM_CHAR chCharCode:'99' (99) cRepeat:1 ScanCode:2E fExtended:0 fAltDown:0 fRepeat:0 fUp:0
<000003> 000204B6 P WM_KEYUP nVirtKey:'C' cRepeat:1 ScanCode:2E fExtended:0 fAltDown:0 fRepeat:1 fUp:1
这是我的代码发送时的spy++
输出
<000001> 000906D4 P WM_KEYDOWN nVirtKey:'C' cRepeat:0 ScanCode:00 fExtended:0 fAltDown:0 fRepeat:0 fUp:0
<000002> 000906D4 P WM_CHAR chCharCode:'99' (99) cRepeat:0 ScanCode:00 fExtended:0 fAltDown:0 fRepeat:0 fUp:0
<000003> 000906D4 P WM_KEYUP nVirtKey:'C' cRepeat:0 ScanCode:00 fExtended:0 fAltDown:0 fRepeat:0 fUp:0
<000004> 000906D4 P WM_CHAR chCharCode:'99' (99) cRepeat:0 ScanCode:00 fExtended:0 fAltDown:0 fRepeat:0 fUp:0
<000005> 000906D4 P WM_CHAR chCharCode:'99' (99) cRepeat:0 ScanCode:00 fExtended:0 fAltDown:0 fRepeat:0 fUp:0
我在这里做错了什么?
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool PostMessage(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);
const int WM_KEYDOWN = 0x100;
const int WM_KEYUP = 0x101;
const Int32 WM_CHAR = 0x0102;
IntPtr val = new IntPtr((Int32)'c');
PostMessage(WindowHandle, WM_KEYDOWN, (IntPtr)(val - 0x020), new IntPtr(0));
PostMessage(WindowHandle, WM_CHAR, (IntPtr)val, new IntPtr(0));
PostMessage(WindowHandle, WM_KEYUP, (IntPtr)(val - 0x020), new IntPtr(0));
想通了。我只需要以下内容!
char Letter = 'a';
PostMessage(WindowHandle, WM_CHAR, (IntPtr)Letter, new IntPtr(0));