为什么PostMessage在发送小写密钥时会发送多个密钥?

时间:2016-06-19 06:09:22

标签: c# automation unmanaged sendmessage postmessage

我使用PostMessage将密钥发送到最小化的firefox实例。下面的代码适用于大写字符,但是当我发送小写时,它似乎发送了每个字符的3个。

这是发送" c"字符。我知道它不是firefox,因为它在记事本中也是如此。

当我手动将输入发送到窗口时,这是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));

1 个答案:

答案 0 :(得分:2)

想出来了。我只需要以下内容!

char Letter = 'a';
PostMessage(WindowHandle, WM_CHAR, (IntPtr)Letter, new IntPtr(0));