Delphi 怎样用postMessage向一个窗口发送Alt+D组合键

怎样用postMessage向一个窗口发送Alt+D组合键
找不到VK_ALT

第1个回答  推荐于2016-05-14
不能简单的Postmessge发送消息!因为组合组合键生成的是一系列消息!

实现方法原理如同楼上所写,代码如下:

hwnd=FindWindow(NULL,窗口标题文本); //获得窗口句柄
if Hwnd <> Null then
begin
SetForegroundWindow(hwnd);//设置窗口为当前窗口
keybd_event(VK_ALT,MapVirtualKey(VK_ALT,0),0,0);
sleep(1000);
keybd_event('D',MapVirtualKey(’D’,0),0,0);
sleep(1000);
keybd_event('D',MapVirtualKey(’D’,0),KEYEVENTF_KEYUP,0);
keybd_event(VK_ALT,MapVirtualKey(VK_ALT,0),KEYEVENTF_KEYUP,0);
end;

代码没经测试,你试试!
wish you luck!

-------------------
在哪个单元定义记不清了!你可能是不没引用这个单元!
VK_alt定义是:$20.你试试用这个代替行不!注:没有编译!本回答被提问者采纳
第2个回答  2009-09-26
4步

1、发送Alt键按下;
2、发送D键按下;
3、发送D键放开;
4、发送Alt键放开。
第3个回答  2009-09-26
转自"超级猛料2003"
procedure SendKey(const mKey: Word; mShiftState: TShiftState;

mCount: Integer = 1); { 模拟系统按键;mCount指定按键次数 }

const

cExtended: set of Byte = [VK_UP, VK_DOWN, VK_LEFT, VK_RIGHT, VK_HOME,

VK_END, VK_PRIOR, VK_NEXT, VK_INSERT, VK_DELETE];

procedure pKeyboardEvent(mKey, mScanCode: Byte; mFlags: Longint);

var

vKeyboardMsg: TMsg;

begin

keybd_event(mKey, mScanCode, mFlags, 0);

while PeekMessage(vKeyboardMsg, 0, WM_KEYFIRST, WM_KEYLAST, PM_REMOVE) do

begin

TranslateMessage(vKeyboardMsg);

DispatchMessage(vKeyboardMsg);

end;

end; { pKeyboardEvent }

procedure pSendKeyDown(mKey: Word; mGenUpMsg: Boolean);

var

vScanCode: Byte;

vNumState: Boolean;

vKeyBoardState: TKeyboardState;

begin

if (mKey = VK_NUMLOCK) then begin

vNumState := ByteBool(GetKeyState(VK_NUMLOCK) and 1);

GetKeyBoardState(vKeyBoardState);

if vNumState then

vKeyBoardState[VK_NUMLOCK] := (vKeyBoardState[VK_NUMLOCK] and not 1)

else vKeyBoardState[VK_NUMLOCK] := (vKeyBoardState[VK_NUMLOCK] or 1);

SetKeyBoardState(vKeyBoardState);

Exit;

end;

vScanCode := Lo(MapVirtualKey(mKey, 0));

if (mKey in cExtended) then begin

pKeyboardEvent(mKey, vScanCode, KEYEVENTF_EXTENDEDKEY);

if mGenUpMsg then

pKeyboardEvent(mKey, vScanCode,

KEYEVENTF_EXTENDEDKEY or KEYEVENTF_KEYUP)

end else begin

pKeyboardEvent(mKey, vScanCode, 0);

if mGenUpMsg then pKeyboardEvent(mKey, vScanCode, KEYEVENTF_KEYUP);

end;

end; { pSendKeyDown }

procedure pSendKeyUp(mKey: Word);

var

vScanCode: Byte;

begin

vScanCode := Lo(MapVirtualKey(mKey, 0));

if mKey in cExtended then

pKeyboardEvent(mKey, vScanCode, KEYEVENTF_EXTENDEDKEY and KEYEVENTF_KEYUP)

else pKeyboardEvent(mKey, vScanCode, KEYEVENTF_KEYUP);

end; { pSendKeyUp }

var

I: Integer;

begin

for I := 1 to mCount do begin

if ssShift in mShiftState then pSendKeyDown(VK_SHIFT, False);

if ssCtrl in mShiftState then pSendKeyDown(VK_CONTROL, False);

if ssAlt in mShiftState then pSendKeyDown(VK_MENU, False);

pSendKeyDown(mKey, True);

if ssShift in mShiftState then pSendKeyUp(VK_SHIFT);

if ssCtrl in mShiftState then pSendKeyUp(VK_CONTROL);

if ssAlt in mShiftState then pSendKeyUp(VK_MENU);

end;

end; { SendKey }

///////End Source

///////Begin Demo

procedure TForm1.Button1Click(Sender: TObject);

begin

SendKey(VK_F4, [ssAlt]);

end;

///////End Demo

转自"超级猛料2003"
end;

如何在VB中用PostMessage向指定窗口发送带有修饰键的组合键?
因为应用程序顶层窗口不一定就是接受按键的窗口(如“NotePad.exe”记事本程序,实际接受按键的窗口是Notepad主窗口下的Edit控件),可以通过ClassID或Caption来查找。假设目标窗口的句柄已取得(hwnd),然后要用PostMessage向该窗口发送按键消息,对于普通字符键,直接用WM_CHAR消息最简单,如下:PostMessage h...

delphi 中怎么向一个非活动窗口发送组合键
begin TargetHandle := FindWindow(nil, PChar('Calculator')); {取计算器窗口句柄} if TargetHandle > 0 then {如果句柄有效} PostMessage(TargetHandle, WM_KEYDOWN, Ord('1'), 0); {发送消息} end;

PostMessage如何发送Ctrl组合键
这个问题一开始也困扰了我好几个月,后来终于解决了,怪自己笨。搜索了很多资料,没找到解决方案,我的解决办法是:由于ctrl是公共的,无所谓窗口激活不激活,所以采用keybd_event先给他按住,然后再对某窗口句柄发送第二个键即可。当然,最后得松开它。

VB Postmessage发送字符串问题
下一个点偏移\/控制\/ ALT修饰键与其他键组合如何发送,使用下面的方法(使用Shift和Control API keybd_event):keybd_event VK_SHIFT,H2A,0,0'模拟按下SHIFT键,与H2A是扫描码VK_SHIFT BR \/> PostMessage的hWndMsg,WM_KEYDOWN,VK_A,及H001E0001的模拟按下A键,SHIFT + A产生一个大写的A...

怎么用DELPHI实现后台按键
SendMessage是可以的,但是你要针对对应的窗口去发送消息。参考下面的代码:PostKeyExHWND第一个参数为WebBrowser的控件句柄。procedure PostKeyEx32(key: Word; const shift: TShiftState; specialkey: Boolean);{ Procedure PostKeyEx32 Parameters:key : virtual keycode of the key to send. For...

vb 如何向后台发送组合键ctrl+enter
PostMessage hBtn, WM_KEYDOWN, vbKeyReturn, 0 '向目标句柄发送回车键 PostMessage hBtn, WM_KEYUP, vbKeyReturn, 0 不过发什么指令我这边是随便设的,你要发什么自己改一个发键代码就行了 你要发组合键,那个模拟键弹起要放在最后面,也就是 先模拟按下ctrl 再模拟按下enter 弹起ctrl 弹起...

vb 如何向后台发送组合键ctrl+enter
首先,你必须获取到目标窗口的Hwnd,然后分二步:'第一步,建立一个模块Module1.bas,复制下面代码到模块中,并保存 '===Module1.bas代码=== Option Explicit Private Declare Function SetForegroundWindow Lib "user32"(ByVal hwnd As Long)As Long Private Declare Function SendMessage Lib "user32...

Delphi的消息处理[2]
SendMessage函数可以越过消息队列直接向窗口过程发送 所以当Windows需要立刻返回值时使用SendMessage 当需要不同的应用程序依次处理消息时使用PostMessage 而Perform从本质上和SendMessage相似 它们直接向窗口过程发送 SendMessage Postmessage函数只需要知道窗口的句柄就可以发送消息 所以它们可以向非Delphi窗体发送...

相似回答