CuiCuiTaGE 发表于 2019-12-3 12:03

Delphi全局热键:

unit Unit1;

interface

uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
Vcl.StdCtrls, Vcl.ComCtrls;

type
TForm1 = class(TForm)
    RichEdit1: TRichEdit;
    procedure FormCreate(Sender: TObject);
    procedure hotykey(var msg: TMessage); message WM_HOTKEY;
    procedure FormDestroy(Sender: TObject);
private
    { Private declarations }
public
    { Public declarations }
end;

var
Form1: TForm1;
id1, id2, id3, id4: Integer;         //id1 ctrl+shift+v , id2 ctrl+shift+window+v

implementation

{$R *.dfm}

procedure TForm1.FormDestroy(Sender: TObject);
begin
UnRegisterHotKey(handle, id1);            //ctrl+shift+V
UnRegisterHotKey(handle, id2);            //ctrl+shift+window+V
UnRegisterHotKey(handle, id3);         //ctrl+shift+Alt+Window+V
UnRegisterHotKey(handle, id4);         //Window+S
end;

procedure TForm1.hotykey(var msg: TMessage);   //定义下面这种热键,要先写长的判断,不然没有反应!这就是原因!
begin
if (GetAsyncKeyState(VK_CONTROL) < 0) and (GetAsyncKeyState(VK_SHIFT) < 0) and (GetAsyncKeyState(VK_MENU) and (GetAsyncKeyState(VK_LWIN) and (GetAsyncKeyState(Ord('V')))) < 0) then      //Ctrl+Shift+Alt+Window+V
    ShowMessage('Ctrl+Shift+Alt+Window+V');

if (GetAsyncKeyState(VK_CONTROL) < 0) and (GetAsyncKeyState(VK_SHIFT) < 0) and (GetAsyncKeyState(VK_LWIN) and (GetAsyncKeyState(Ord('V'))) < 0) then      //Ctrl+Shift+Window+V
    ShowMessage('Ctrl+Shift+Window+V');

if (GetAsyncKeyState(VK_LWIN) and (GetAsyncKeyState(Ord('S'))) < 0) then      //Window+V
    ShowMessage('Window+S');


//if (msg.LParamLo = MOD_CONTROL) and (msg.LParamHi = 86) then
if (GetAsyncKeyState(VK_CONTROL) < 0) and (GetAsyncKeyState(VK_SHIFT) < 0) and (GetAsyncKeyState(Ord('V')) < 0) then          //ctrl+shift+v
begin
    ShowMessage('ctrl+shift+v');
    RichEdit1.PasteFromClipboard;
end;
end;

procedure TForm1.FormCreate(Sender: TObject);      //另外 hotkey1 hotkey2 hotkey3要写三个
begin
id1 := GlobalAddAtom('hotkey1');
RegisterHotKey(handle, id1, MOD_CONTROL + MOD_SHIFT, 86);                        //ctrl+shift+V 成功了~~

id2 := GlobalAddAtom('hotkey2');
RegisterHotKey(handle, id2, MOD_CONTROL + MOD_SHIFT + MOD_WIN, 86);                //ctrl+shift+window+V 成功了~~

id3 := GlobalAddAtom('hotkey3');
RegisterHotKey(handle, id3, MOD_CONTROL + MOD_SHIFT + MOD_ALT + MOD_WIN, 86);      //ctrl+shift+Alt+Window+V 成功了~~

id4 := GlobalAddAtom('hotkey4');
RegisterHotKey(handle, id4, MOD_WIN, 83);      //Window+V
end;

end.

diyikuai 发表于 2019-12-3 23:49

我不会这个编程,但是也过来支持一下

blockke 发表于 2020-10-28 10:34

不用控件实现全局热键,good

iyjrcJOU089 发表于 2022-3-1 03:23

谢谢分享

FKEUiMSn0 发表于 2022-3-1 15:12

感谢楼主

KbRDG16 发表于 2022-3-1 15:28

大佬无敌

mbkvpGML4 发表于 2022-3-8 01:07

甘拜下风,谢谢分享

bLIRZrj 发表于 2022-3-8 15:17

被标题吸引进来了,回复看看

Ezy2 发表于 2022-3-8 15:25

谢谢分享

GSkulFOjfAW 发表于 2022-3-8 15:25

感谢楼主
页: [1] 2 3 4 5 6 7
查看完整版本: Delphi全局热键: