我想编一程序实现在另一应用程序中实现按键。(100分)

N

nfy

Unregistered / Unconfirmed
GUEST, unregistred user!
我想编一程序实现在另一应用程序中实现按键。<br>经测试使用postmessage,sendmessage,keybd_event,sendinput等方法在此应用程序中都不能实现按下某键,是不是要非要用钩子啊,哪位好心人,给个示例用钩子在其它应用程序中模拟按键啊。谢谢啊!最好有代码啊
 
你试试用setforegroundow将对象窗口设为当前焦点,再用keybd_event
 
先用findwindowex找到按钮的句柄,然后向这个句柄发送消息。
 
我试过用postmessage在传键值给KOF,不过它会暂停弹出个对话框写着‘key..........'<br>keybd_event也试过,好像不行!<br>
 
在其它应用程序中<br>keybd_event好像是获得输入焦点才好使,<br>postmessage好像是获得输入焦点才好使,<br>sendinput能模拟回车,但是c 2 3 这样的按键也不好使。<br>楼上的兄弟的情况可以注册热键,实现1键出绝杀什么的。<br>但我的情况,是不是非得用钩子啊,谁有SendInput()的应用范例也行啊。
 
使用消息
 
提供一些MSDN资料。<br>其中我测试过模拟鼠标移动,是确实可行的。但没有测试模拟键盘按键,你不妨试一下。<br><br><br>移动鼠标光标可以考虑API函数SetCursorPos<br>先说几个API函数<br>keybd_event<br>The keybd_event function synthesizes a keystroke. The system can use such a synthesized keystroke to generate a WM_KEYUP or WM_KEYDOWN message. The keyboard driver's interrupt handler calls the keybd_event function. <br><br>Windows NT: This function has been superseded. Use SendInput instead.<br><br>VOID keybd_event(<br>&nbsp; BYTE bVk, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // virtual-key code<br>&nbsp; BYTE bScan, &nbsp; &nbsp; &nbsp; &nbsp; // hardware scan code<br>&nbsp; DWORD dwFlags, &nbsp; &nbsp; &nbsp;// flags specifying various function options<br>&nbsp; DWORD dwExtraInfo &nbsp; // additional data associated with keystroke<br>);<br>&nbsp;<br>Parameters<br>bVk <br>Specifies a virtual-key code. The code must be a value in the range 1 to 254. <br>bScan <br>Specifies a hardware scan code for the key. <br>dwFlags <br>A set of flag bits that specify various aspects of function operation. An application can use any combination of the following predefined constant values to set the flags. Value Meaning <br>KEYEVENTF_EXTENDEDKEY If specified, the scan code was preceded by a prefix byte having the value 0xE0 (224). <br>KEYEVENTF_KEYUP If specified, the key is being released. If not specified, the key is being depressed. <br><br><br>dwExtraInfo <br>Specifies an additional 32-bit value associated with the key stroke. <br>Return Values<br>This function has no return value. <br><br>Remarks<br>Although keybd_event passes an OEM-dependent hardware scan code to the system, applications should not use the scan code. The system converts scan codes to virtual-key codes internally and clears the up/down bit in the scan code before passing it to applications. <br><br>An application can simulate a press of the PRINTSCREEN key in order to obtain a screen snapshot and save it to the clipboard. To do this, call keybd_event with the bVk parameter set to VK_SNAPSHOT, and the bScan parameter set to 0 for a snapshot of the full screen or set bScan to 1 for a snapshot of the active window. <br><br>Windows CE: Windows CE supports an additional flag for the dwFlags parameter. Use the KEYEVENTF_SILENT flag to simulate a keystroke without making a clicking sound.<br><br>Windows CE does not support the KEYEVENTF_EXTENDEDKEY flag.<br><br>QuickInfo<br>&nbsp; Windows NT: Requires version 3.1 or later.<br>&nbsp; Windows: Requires Windows 95 or later.<br>&nbsp; Windows CE: Requires version 1.0 or later.<br>&nbsp; Header: Declared in winuser.h.<br>&nbsp; Import Library: Use user32.lib.<br><br>See Also<br>Keyboard Input Overview, Keyboard Input Functions, GetAsyncKeyState, GetKeyState, MapVirtualKey, SetKeyboardState <br><br><br>mouse_event<br>The mouse_event function synthesizes mouse motion and button clicks. <br><br>Windows NT: This function has been superseded. Use SendInput instead.<br><br>VOID mouse_event(<br>&nbsp; DWORD dwFlags, // flags specifying various motion/click variants<br>&nbsp; DWORD dx, &nbsp; &nbsp; &nbsp;// horizontal mouse position or position change<br>&nbsp; DWORD dy, &nbsp; &nbsp; &nbsp;// vertical mouse position or position change<br>&nbsp; DWORD dwData, &nbsp;// amount of wheel movement<br>&nbsp; DWORD dwExtraInfo <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// 32 bits of application-defined information<br>);<br>&nbsp;<br>Parameters<br>dwFlags <br>A set of flag bits that specify various aspects of mouse motion and button clicking. The bits in this parameter can be any reasonable combination of the following values: Value Meaning <br>MOUSEEVENTF_ABSOLUTE Specifies that the dx and dy parameters contain normalized absolute coordinates. If not set, those parameters contain relative data: the change in position since the last reported position. This flag can be set, or not set, regardless of what kind of mouse or mouse-like device, if any, is connected to the system. For further information about relative mouse motion, see the following Remarks section. <br>MOUSEEVENTF_MOVE Specifies that movement occurred. <br>MOUSEEVENTF_LEFTDOWN Specifies that the left button is down. <br>MOUSEEVENTF_LEFTUP Specifies that the left button is up. <br>MOUSEEVENTF_RIGHTDOWN Specifies that the right button is down. <br>MOUSEEVENTF_RIGHTUP Specifies that the right button is up. <br>MOUSEEVENTF_MIDDLEDOWN Specifies that the middle button is down. <br>MOUSEEVENTF_MIDDLEUP Specifies that the middle button is up. <br>MOUSEEVENTF_WHEEL Windows NT: Specifies that the wheel has been moved, if the mouse has a wheel. The amount of movement is given in dwData <br><br><br>dx <br>Specifies the mouse's absolute position along the x-axis or its amount of motion since the last mouse event was generated, depending on the setting of MOUSEEVENTF_ABSOLUTE. Absolute data is given as the mouse's actual x-coordinate; relative data is given as the number of mickeys moved. A mickey is the amount that a mouse has to move for it to report that it has moved. <br>dy <br>Specifies the mouse's absolute position along the y-axis or its amount of motion since the last mouse event was generated, depending on the setting of MOUSEEVENTF_ABSOLUTE. Absolute data is given as the mouse's actual y-coordinate; relative data is given as the number of mickeys moved. <br>dwData <br>If dwFlags is MOUSEEVENTF_WHEEL, then dwData specifies the amount of wheel movement. A positive value indicates that the wheel was rotated forward, away from the user; a negative value indicates that the wheel was rotated backward, toward the user. One wheel click is defined as WHEEL_DELTA, which is 120. <br>If dwFlags is not MOUSEEVENTF_WHEEL, then dwData should be zero. <br><br>dwExtraInfo <br>Specifies an additional 32-bit value associated with the mouse event. An application calls GetMessageExtraInfo to obtain this extra information. <br>Return Values<br>This function has no return value. <br><br>Remarks<br>If the mouse has moved, indicated by MOUSEEVENTF_MOVE being set, dx and dy hold information about that motion. The information is given as absolute or relative integer values. <br><br>If MOUSEEVENTF_ABSOLUTE value is specified, dx and dy contain normalized absolute coordinates between 0 and 65,535. The event procedure maps these coordinates onto the display surface. Coordinate (0,0) maps onto the upper-left corner of the display surface, (65535,65535) maps onto the lower-right corner. <br><br>If the MOUSEEVENTF_ABSOLUTE value is not specified, dx and dy specify relative motions from when the last mouse event was generated (the last reported position). Positive values mean the mouse moved right (or down); negative values mean the mouse moved left (or up). <br><br>Relative mouse motion is subject to the settings for mouse speed and acceleration level. An end user sets these values using the Mouse control panel application. An application obtains and sets these values with theSystemParametersInfo function. <br><br>The system applies two tests to the specified relative mouse motion when applying acceleration. If the specified distance along either the x or y axis is greater than the first mouse threshold value, and the mouse acceleration level is not zero, the operating system doubles the distance. If the specified distance along either the x or y axis is greater than the second mouse threshold value, and the mouse acceleration level is equal to two, the operating system doubles the distance that resulted from applying the first threshold test. It is thus possible for the operating system to multiply relatively-specified mouse motion along the x or y axis by up to four times.<br><br>Once acceleration has been applied, the system scales the resultant value by the desired mouse speed. Mouse speed can range from 1 (slowest) to 20 (fastest) and represents how much the pointer moves based on the distance the mouse moves. The default value is 10, which results in no additional modification to the mouse motion. <br><br>The mouse_event function is used to synthesize mouse events by applications that need to do so. It is also used by applications that need to obtain more information from the mouse than its position and button state. For example, if a tablet manufacturer wants to pass pen-based information to its own applications, it can write a dynamic-link library (DLL) that communicates directly to the tablet hardware, obtains the extra information, and saves it in a queue. The DLL then calls mouse_event with the standard button and x/y position data, along with, in the dwExtraInfo parameter, some pointer or index to the queued extra information. When the application needs the extra information, it calls the DLL with the pointer or index stored in dwExtraInfo, and the DLL returns the extra information. <br><br>Windows CE: Windows CE does not support the MOUSEEVENTF_WHEEL constant in the dwFlags parameter.<br><br>QuickInfo<br>&nbsp; Windows NT: Requires version 3.1 or later.<br>&nbsp; Windows: Requires Windows 95 or later.<br>&nbsp; Windows CE: Unsupported.<br>&nbsp; Header: Declared in winuser.h.<br>&nbsp; Import Library: Use user32.lib.<br><br>See Also<br>Mouse Input Overview, Mouse Input Functions, GetMessageExtraInfo,SystemParametersInfo<br><br>&nbsp;-摘自MSDN<br>Keyboard input &nbsp;及 Mouse Input部分<br>&nbsp;<br>
 
没人知道了吗?
 
我抄的,你可以看一下这里:http://www.delphibbs.com/delphibbs/dispq.asp?lid=1407832<br>--------------------------------<br>unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br>&nbsp; Dialogs, StdCtrls;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br><br>implementation<br><br>{$R *.dfm}<br><br>procedure SendKey(const mKey: Word; mShiftState: TShiftState;<br>&nbsp; mCount: Integer = 1); { 模拟系统按键;mCount指定按键次数 }<br>const<br>&nbsp; cExtended: set of Byte = [VK_UP, VK_DOWN, VK_LEFT, VK_RIGHT, VK_HOME,<br>&nbsp; &nbsp; VK_END, VK_PRIOR, VK_NEXT, VK_INSERT, VK_DELETE];<br><br>&nbsp; procedure pKeyboardEvent(mKey, mScanCode: Byte; mFlags: Longint);<br>&nbsp; var<br>&nbsp; &nbsp; vKeyboardMsg: TMsg;<br>&nbsp; begin<br>&nbsp; &nbsp; keybd_event(mKey, mScanCode, mFlags, 0);<br>&nbsp; &nbsp; while PeekMessage(vKeyboardMsg, 0, WM_KEYFIRST, WM_KEYLAST, PM_REMOVE) do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; TranslateMessage(vKeyboardMsg);<br>&nbsp; &nbsp; &nbsp; DispatchMessage(vKeyboardMsg);<br>&nbsp; &nbsp; end;<br>&nbsp; end; { pKeyboardEvent }<br><br>&nbsp; procedure pSendKeyDown(mKey: Word; mGenUpMsg: Boolean);<br>&nbsp; var<br>&nbsp; &nbsp; vScanCode: Byte;<br>&nbsp; &nbsp; vNumState: Boolean;<br>&nbsp; &nbsp; vKeyBoardState: TKeyboardState;<br>&nbsp; begin<br>&nbsp; &nbsp; if (mKey = VK_NUMLOCK) then begin<br>&nbsp; &nbsp; &nbsp; vNumState := ByteBool(GetKeyState(VK_NUMLOCK) and 1);<br>&nbsp; &nbsp; &nbsp; GetKeyBoardState(vKeyBoardState);<br>&nbsp; &nbsp; &nbsp; if vNumState then<br>&nbsp; &nbsp; &nbsp; &nbsp; vKeyBoardState[VK_NUMLOCK] := (vKeyBoardState[VK_NUMLOCK] and not 1)<br>&nbsp; &nbsp; &nbsp; else vKeyBoardState[VK_NUMLOCK] := (vKeyBoardState[VK_NUMLOCK] or 1);<br>&nbsp; &nbsp; &nbsp; SetKeyBoardState(vKeyBoardState);<br>&nbsp; &nbsp; &nbsp; Exit;<br>&nbsp; &nbsp; end;<br><br>&nbsp; &nbsp; vScanCode := Lo(MapVirtualKey(mKey, 0));<br>&nbsp; &nbsp; if (mKey in cExtended) then begin<br>&nbsp; &nbsp; &nbsp; pKeyboardEvent(mKey, vScanCode, KEYEVENTF_EXTENDEDKEY);<br>&nbsp; &nbsp; &nbsp; if mGenUpMsg then<br>&nbsp; &nbsp; &nbsp; &nbsp; pKeyboardEvent(mKey, vScanCode,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; KEYEVENTF_EXTENDEDKEY or KEYEVENTF_KEYUP)<br>&nbsp; &nbsp; end else begin<br>&nbsp; &nbsp; &nbsp; pKeyboardEvent(mKey, vScanCode, 0);<br>&nbsp; &nbsp; &nbsp; if mGenUpMsg then pKeyboardEvent(mKey, vScanCode, KEYEVENTF_KEYUP);<br>&nbsp; &nbsp; end;<br>&nbsp; end; { pSendKeyDown }<br><br>&nbsp; procedure pSendKeyUp(mKey: Word);<br>&nbsp; var<br>&nbsp; &nbsp; vScanCode: Byte;<br>&nbsp; begin<br>&nbsp; &nbsp; vScanCode := Lo(MapVirtualKey(mKey, 0));<br>&nbsp; &nbsp; if mKey in cExtended then<br>&nbsp; &nbsp; &nbsp; pKeyboardEvent(mKey, vScanCode, KEYEVENTF_EXTENDEDKEY and KEYEVENTF_KEYUP)<br>&nbsp; &nbsp; else pKeyboardEvent(mKey, vScanCode, KEYEVENTF_KEYUP);<br>&nbsp; end; { pSendKeyUp }<br><br>var<br>&nbsp; I: Integer;<br>begin<br>&nbsp; for I := 1 to mCount do begin<br>&nbsp; &nbsp; if ssShift in mShiftState then pSendKeyDown(VK_SHIFT, False);<br>&nbsp; &nbsp; if ssCtrl in mShiftState then pSendKeyDown(VK_CONTROL, False);<br>&nbsp; &nbsp; if ssAlt in mShiftState then pSendKeyDown(VK_MENU, False);<br>&nbsp; &nbsp; pSendKeyDown(mKey, True);<br>&nbsp; &nbsp; if ssShift in mShiftState then pSendKeyUp(VK_SHIFT);<br>&nbsp; &nbsp; if ssCtrl in mShiftState then pSendKeyUp(VK_CONTROL);<br>&nbsp; &nbsp; if ssAlt in mShiftState then pSendKeyUp(VK_MENU);<br>&nbsp; end;<br>end; { SendKey }<br>///////End Source<br><br>///////Begin Demo<br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br>&nbsp; SendKey(VK_F4, [ssAlt]);<br>end;<br>///////End Demo<br><br>end.<br>
 
首先获得另一个程序主窗口及相关控件的句柄!然后用postmessage等进行处理。
 
那位大侠告诉你用钩子的?他绝对是个骗子。。(得罪了)<br>jsxjd说得对,没句柄你搞个p。。搞到句柄后随便你了,用钩子那就是自己玩就行了吗<br>,还要其他程序控制干吗?无聊。。除非要写一套自己的消息。。用你那个<br>控制程序做代理,累不累啊,那不是白痴吗?<br>
 
procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; Wnd: HWND;<br>begin<br><br>&nbsp; Wnd := FindWindow('notepad',nil);<br>&nbsp; if Wnd &lt;&gt; 0 then<br>&nbsp; begin<br>&nbsp; &nbsp; SetForegroundWindow(Wnd);<br>&nbsp; &nbsp; keybd_event(VK_TAB, MapVirtualKey(VK_TAB, 0), 0, 0);<br>&nbsp; &nbsp; keybd_event(VK_TAB, MapVirtualKey(VK_TAB, 0), KEYEVENTF_KEYUP, 0);<br>&nbsp; end;<br><br>end;
 
各位大侠请注意,此程序是个网络游戏,要控制的是按数字键1 2 3{不是小键盘数字} 等,是没有输入焦点。<br>我在代码是这样的<br>var &nbsp;HWndCalculator : HWnd;<br><br>begin<br>&nbsp; &nbsp;HWndCalculator:=FindWindow(nil,'xxxx');<br>&nbsp; &nbsp;SetForegroundWindow(HWndCalculator);<br>&nbsp; &nbsp;postmessage(HWndCalculator,Wm_keydown,52,0);{4键}<br>end;<br>但是都不能实现按下4键,包括SendMessage(),key_Event()都不行,mouse_Event()好使。<br>SendInput()可以模拟回车键按下,但其它键模拟不了。不知是不是我的用法不对?<br>我想可能是这些键已被游戏注册为热键了吧, 可以参考按键精灵(可能用到钩子),EZScript(没用钩子,台湾人编的)的功能,我就是想实现他们那样的功能,如有谁知道实现的原理,请告之!谢谢!<br>若用钩子,给我讲讲模拟钩子的原理。
 
钩子这玩艺对学过vc的来说太easy了<br>看我回答的一个帖子去(用delphi实现)<br>ID:1439446(在olecontainer中如何控制嵌入的对象,如屏蔽word文档的拷贝,复制功能等)
 
好啦給分吧,下面是我抄的一個涵數,抄誰的我忘了,但是還請那位仁兄不要見怪,賺點分而已:<br>procedure TSysUser.SendKeys(sSend: string);<br>var<br>&nbsp; i: integer;<br>&nbsp; focushld, windowhld: hwnd;<br>&nbsp; threadld: dword;<br>&nbsp; ch: byte;<br>begin<br>&nbsp; windowhld := GetForegroundWindow;<br>&nbsp; threadld := GetWindowThreadProcessId(Windowhld, nil);<br>&nbsp; AttachThreadInput(GetCurrentThreadId, threadld, true);<br>&nbsp; Focushld := getfocus;<br>&nbsp; AttachThreadInput(GetCurrentThreadId, threadld, false);<br>&nbsp; if focushld = 0 then Exit;<br>&nbsp; i := 1;<br>&nbsp; while i &lt;= Length(sSend) do<br>&nbsp; begin<br>&nbsp; &nbsp; ch := byte(sSend); //send string//<br>&nbsp; &nbsp; if Windows.IsDBCSLeadByte(ch) then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; Inc(i);<br>&nbsp; &nbsp; &nbsp; SendMessage(focushld, WM_IME_CHAR, MakeWord(byte(sSend), ch), 0);<br>&nbsp; &nbsp; end<br>&nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; SendMessage(focushld, WM_IME_CHAR, word(ch), 0);<br>&nbsp; &nbsp; Inc(i);<br>&nbsp; end;<br>&nbsp; postmessage(focushld, WM_keydown, 13, 0); //enter//<br>end;
 
如果没有句柄,你就在坐标上模拟单击了,谁说用钩子,打出去。
 
Study <br><br>应该可以<br>
 
我觉得bike的方法应该行
 
顶部