使用的User32.dll SendMessage函数发送键使用ALT修改 [英] Using User32.dll SendMessage To Send Keys With ALT Modifier

查看:1108
本文介绍了使用的User32.dll SendMessage函数发送键使用ALT修改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
C#和SendMessage消息(键)不工作

我写发出按键来使用的 user32.dll中的定义SendMessage函数的另一个应用程序的应用程序。我已经想通了如何发送一个按键,但我难倒尝试发送按键与ALT键一起。

I am writing an application that sends keystrokes to another application using the SendMessage function defined in user32.dll. I have figured out how to send a single keystroke but I am stumped trying to send the keystroke along with the ALT key.

有关我的问题的目的,我将专注于发送F1和ALT + F1。

For the purposes of my question I will focus on sending F1, and ALT + F1.

如上所述,我能送F1键没有问题。这里是发送F1键我的代码片段:

As stated above, I am able to send the F1 key no problem. Here is a snippet of my code that sends the F1 key:

// DLL Imports

//Set the active window
[DllImport("user32.dll")]
public static extern IntPtr SetActiveWindow(IntPtr hWnd);

//sends a windows message to the specified window
[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, uint wParam, uint lParam);

// ...

// Some constants
#define WM_SYSKEYDOWN 260
#define WM_SYSKEYUP 261
#define WM_CHAR 258
#define WM_KEYDOWN 256
#define WM_KEYUP 257

// ...

// activate the window and send F1
SetActiveWindow(hWnd);
ushort action = (ushort)WM_SYSKEYDOWN;
ushort key = (ushort)System.Windows.Forms.Keys.F1;
SendMessage(hWnd, action, key, 0);



一个有趣的一面值得注意的是,即使上面的代码工作在发送F1键到目标应用程序它是不一样的我看到使用间谍++。这里是间谍的输出++日志每当我打在监视目标应用程序的F1键:

One interesting side note is that even though the above code works in sending the F1 key to the target application it is not the same as what I see using Spy++. Here is the output of the Spy++ log whenever I hit the F1 key while monitoring the target application:

<00001> 00050412 P WM_KEYDOWN nVirtKey:VK_F1 cRepeat:1 ScanCode:3B fExtended:0 fAltDown:0 fRepeat:0 fUp:0
<00002> 00050412 P WM_KEYUP nVirtKey:VK_F1 cRepeat:1 ScanCode:3B fExtended:0 fAltDown:0 fRepeat:1 fUp:1

请注意,有派,WM_KEYDOWN和WM_KEYUP两个消息。

Note that there are two messages sent, WM_KEYDOWN and WM_KEYUP.

*我的第一个问题是,为什么我成功的使用WM_SYSKEYDOWN当间谍++告诉我,WM_KEYDOWN + WM_KEYUP是正确的消息序列发送F1 *?

*My first question would be, why am I succesful sending F1 using WM_SYSKEYDOWN when Spy++ tells me that WM_KEYDOWN + WM_KEYUP is the proper message sequence?*

移动上的尝试发送ALT + F1我的下一个挑战。

Moving on to my next challenge of trying to send ALT + F1.

我用间谍++监视的消息通过我的键盘上按ALT + F1的时候,这就是我看到的:

I have used Spy++ to monitor the messages passed when pressing ALT + F1 on my keyboard and this is what I see:

<00001> 00050412 P WM_SYSKEYDOWN nVirtKey:VK_MENU cRepeat:1 ScanCode:38 fExtended:1 fAltDown:1 fRepeat:0 fUp:0
<00002> 00050412 P WM_SYSKEYDOWN nVirtKey:VK_F1 cRepeat:1 ScanCode:3B fExtended:0 fAltDown:1 fRepeat:0 fUp:0
<00003> 00050412 P WM_SYSKEYUP nVirtKey:VK_F1 cRepeat:1 ScanCode:3B fExtended:0 fAltDown:1 fRepeat:1 fUp:1
<00004> 00050412 P WM_KEYUP nVirtKey:VK_MENU cRepeat:1 ScanCode:38 fExtended:1 fAltDown:0 fRepeat:1 fUp:1

鉴于上述间谍++邮件捕获我试着用下面的代码(简化)发送的确切消息序列:

Given the above Spy++ message capture I tried to send the exact message sequence using the following code (simplified):

SetActiveWindow(hWnd);    
SendMessage(hWnd, (ushort)WM_SYSKEYDOWN, (ushort)System.Windows.Forms.Keys.Menu, 0);
SendMessage(hWnd, (ushort)WM_SYSKEYDOWN, (ushort)System.Windows.Forms.Keys.F1, 0);
SendMessage(hWnd, (ushort)WM_SYSKEYUP, (ushort)System.Windows.Forms.Keys.F1, 0);
SendMessage(hWnd, (ushort)WM_KEYUP, (ushort)System.Windows.Forms.Keys.Menu, 0);

这并不工作。

和所以这导致了我的下一个问题。的还有什么我可以试试或者是有什么,我做错了什么?

And so this leads to my next question. Is there anything else I can try or is there something that I am doing wrong here?

每当我俘获了我的程序使用间谍++输出这里是被记录的:

Whenever I captured the output of my program using Spy++ here is what was logged:

<00001> 00050412 S WM_SYSKEYDOWN nVirtKey:VK_MENU cRepeat:0 ScanCode:00 fExtended:0 fAltDown:0 fRepeat:0 fUp:0
<00002> 00050412 R WM_SYSKEYDOWN
<00003> 00050412 S WM_SYSKEYDOWN nVirtKey:VK_F1 cRepeat:0 ScanCode:00 fExtended:0 fAltDown:0 fRepeat:0 fUp:0
<00004> 00050412 R WM_SYSKEYDOWN
<00005> 00050412 S WM_SYSKEYUP nVirtKey:VK_F1 cRepeat:0 ScanCode:00 fExtended:0 fAltDown:0 fRepeat:0 fUp:0
<00006> 00050412 R WM_SYSKEYUP
<00007> 00050412 S WM_KEYUP nVirtKey:VK_MENU cRepeat:0 ScanCode:00 fExtended:0 fAltDown:0 fRepeat:0 fUp:0
<00008> 00050412 R WM_KEYUP



注意到有额外的的消息被以线2发,4,6和8的可这是为什么事情不工作的原因?

Notice that there are extra messages being sent at lines 2, 4, 6, and 8. Could this be the reason why things are not working?

我有一个关于之间的差别最后一个问题从实际的键盘输入和那些使用我的应用程序捕获抓获的消息。注意的 cRepeat 扫描码 fExtended 的等的参数的。它们是在分别使用我的键盘作为输入的拍摄
中的消息的非零和它们在我的应用程序发送的消息的所有零。 可这是为什么我的代码不工作的原因?的*如果是这样,我怎么修改这些值?*(我假设他们来自第四个参数为SendMessage函数
功能,我已经在所有的情况下设置为零。)

I have one final question regarding the difference between the messages captured from actual keyboard input and those captured using my application. Notice the cRepeat, ScanCode, fExtended, etc. arguments. They are non-zero in the messages that were captured using my keyboard as the input and they are all zero in the messages sent by my application. Could this be the reason why my code is not working? *If so, how do I modify these values?* (I am assuming they come from the 4th argument to the SendMessage function, which I have set to zero in all cases.)

感谢您,

推荐答案

另一种解决方案。它不会出现你递东西的WM_SYSKEYDOWN的LPARAM。然而的文档,清楚地表明,该LPARAM的29位需要设置以指示ALT键被按下

Another solution. It doesn't appear you are passing anything for the lparam of the WM_SYSKEYDOWN. Yet the docs, clearly suggest that bit 29 of the lparam needs to be set to indicate the ALT key was pressed.

ushort action = (ushort)WM_SYSKEYDOWN;
ushort key = (ushort)System.Windows.Forms.Keys.F1;
uint lparam = (0x01 << 28);
SendMessage(hWnd, action, key, lparam);

这篇关于使用的User32.dll SendMessage函数发送键使用ALT修改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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