MSDN:
mouse_event
The mouse_event function synthesizes mouse motion and button clicks. Windows NT: This function has been superseded. Use SendInput instead.VOID mouse_event(
DWORD dwFlags, // flags specifying various motion/click variants
DWORD dx, // horizontal mouse position or position change
DWORD dy, // vertical mouse position or position change
DWORD dwData, // amount of wheel movement
DWORD dwExtraInfo
// 32 bits of application-defined information
);
Parameters
dwFlags
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
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 Res section.
MOUSEEVENTF_MOVE Specifies that movement occurred.
MOUSEEVENTF_LEFTDOWN Specifies that the left button is down.
MOUSEEVENTF_LEFTUP Specifies that the left button is up.
MOUSEEVENTF_RIGHTDOWN Specifies that the right button is down.
MOUSEEVENTF_RIGHTUP Specifies that the right button is up.
MOUSEEVENTF_MIDDLEDOWN Specifies that the middle button is down.
MOUSEEVENTF_MIDDLEUP Specifies that the middle button is up.
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
dx
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.
dy
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.
dwData
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.
If dwFlags is not MOUSEEVENTF_WHEEL, then dwData should be zero. dwExtraInfo
Specifies an additional 32-bit value associated with the mouse event. An application calls GetMessageExtraInfo to obtain this extra information.
mouse_event
The mouse_event function synthesizes mouse motion and button clicks. Windows NT: This function has been superseded. Use SendInput instead.VOID mouse_event(
DWORD dwFlags, // flags specifying various motion/click variants
DWORD dx, // horizontal mouse position or position change
DWORD dy, // vertical mouse position or position change
DWORD dwData, // amount of wheel movement
DWORD dwExtraInfo
// 32 bits of application-defined information
);
Parameters
dwFlags
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
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 Res section.
MOUSEEVENTF_MOVE Specifies that movement occurred.
MOUSEEVENTF_LEFTDOWN Specifies that the left button is down.
MOUSEEVENTF_LEFTUP Specifies that the left button is up.
MOUSEEVENTF_RIGHTDOWN Specifies that the right button is down.
MOUSEEVENTF_RIGHTUP Specifies that the right button is up.
MOUSEEVENTF_MIDDLEDOWN Specifies that the middle button is down.
MOUSEEVENTF_MIDDLEUP Specifies that the middle button is up.
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
dx
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.
dy
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.
dwData
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.
If dwFlags is not MOUSEEVENTF_WHEEL, then dwData should be zero. dwExtraInfo
Specifies an additional 32-bit value associated with the mouse event. An application calls GetMessageExtraInfo to obtain this extra information.
HWND hWnd, //响应该热键的窗口句柄
Int id, //该热键的唯一标识
UINT fsModifiers, //该热键的辅助按键
UINT vk //该热键的键值
); 为了得到唯一标识,我们还将用到另一个API函数 ATOM GlobalAddAtom(LPCTSTR lpString //自己设定的一个字符串);因为我们还要在程序退出的时候,消除这个热键, 所以需要声明一个全局变量: HotKeyId: Integer; 第一步: 在窗口的create事件中,加入以下代码 HotKeyId := GlobalAddAtom(‘MyHotKey’) - $C000;
注: HotKeyId的合法取之范围是0x0000到0xBFFF之间, GlobalAddAtom函数得到的值
在0xC000到0xFFFF之间,所以减掉0xC000来满足调用要求。第二步: 在上面的代码下面加入: RegisterHotKey(Handle, hotkeyid, MOD_ALT, VK_F8); 热键的辅助按键包括Mod_Ctrl 、Mod_Alt、Mod_Shift,对于Windows兼容键盘还支持Windows
键,即其键面上有Windows标志的那个键,其值为Mod_win。上面 的代码注册了一个热键:ALT+F8。当然如果你希望象TAKEIT那样,只用F8, 就这么写:RegisterHotKey(Handle, hotkeyid, 0, VK_F8); 注:handle是一个特殊的变量,它表示当前窗口的句柄。这个函数你应该能句举一反三了吧。原理:一旦热键设置成功,在程序应用过程中如果有相应的键被按下,Windows系统都会给你的应
用程序发送一个消息WM_HOTKEY,不管你的应用程序是否为当前活动的。其中WM_HOTKEY消
息的格式为: idHotKey = (int) wParam; // 该参数在设置系统级的热键有用,一般不予使用
fuModifiers = (UINT) LOWORD(lParam); //热键的辅助按键
uVirtKey = (UINT) HIWORD(lParam); //热键的键值 第三步: 注册了热键,就该写下响应代码了。 首先,在程序头部分的private段中加入声明 (作用是声明这个过程,和声明变量类似。
关于如何声明函数、过程,请请参考各自的帮助文件或其它资料): procedure HotKeyDown(var Msg: Tmessage); message WM_HOTKEY; 然后在程序中加入如下代码: procedure Tfmain.HotKeyDown(var Msg: Tmessage); begin if (Msg.LparamLo = MOD_ALT) AND Msg.LParamHi = VK_F8 then // 假设热键为ALT+F8 begin end; 最后一步: 在窗口的close事件中加入 UnRegisterHotKey(handle, HotKeyId); //注销HotKey, 释放资源
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure OnHotKey(var msg:Tmessage);message WM_HOTKEY;
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.OnHotKey(var msg:Tmessage);
var
pt:TPoint;
begin
if msg.WParam=9999 then
begin
GetCursorPos(pt);
mouse_event(MOUSEEVENTF_LEFTDOWN,pt.x,pt.y,0,0);
mouse_event(MOUSEEVENTF_LEFTUP,pt.x,pt.y,0,0);
end;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
RegisterHotkey(handle,9999,0,VK_RETURN); //注册一个id为9999的hotkey
end;procedure TForm1.FormDestroy(Sender: TObject);
begin
UnRegisterHotkey(handle,9999);
end;end.