用什么办法可以得知按下的是键盘上的哪个键(任意键)?如怎么知道按下的是左边的win键还有右边的win键?

xiaoqlj 2003-09-24 02:10:04
用什么办法可以得知按下的是键盘上的哪个键(任意键)?如怎么知道按下的是左边的win键还有右边的win键?大小写等。
...全文
1054 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
FrameSniper 2003-09-24
  • 打赏
  • 举报
回复
呵呵,揭贴吧!
ollea 2003-09-24
  • 打赏
  • 举报
回复
2.创建调用DLL的EXE程序HOOK.EXE

  * 选择FILE菜单中的NEW选项,在New Items窗口中,选择Application选项。在窗体Form中,加入两个按键,一个定义为挂钩,另一个定义为解脱,同时加入一个文本框以提示挂钩的设置状况。将Unit1存为“c:\hook\hk.pas”,其相应的代码如下:

  

  unit hk;

  interface

  

  uses

   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,

   StdCtrls;

  

  type

   TForm1 = class(TForm)

   Button1: TButton;

   Button2: TButton;

   Edit1: TEdit;

   procedure Button1Click(Sender: TObject);

   procedure Button2Click(Sender: TObject);

  

   private

   { Private declarations }

   public

   { Public declarations }

   end;

  

  var

   Form1: TForm1;

  

  implementation

  

  {$R *.DFM}

  

  function EnableHotKeyHook : BOOL;external 'HK.dll';

  //声明HOOK . DLL中的两函数

  function DisableHotKeyHook :BOOL;external 'HK.dll';

  

  procedure TForm1.Button1Click(Sender: TObject);

  begin

   if EnableHotKeyHook() then

   begin

   edit1.text :='设置挂钩'

   end

  end;

  

  procedure TForm1.Button2Click(Sender: TObject);

  begin

   if DisableHotKeyHook() then

   begin

   edit1.Text :='挂钩解脱'

   end

  end;

  end.

  * 选取Views菜单中的Project Source,将Project1存为“c:\hook\hook.dpr”,其代码如下:

  

  program hook;

  uses

   Forms,

   hk in 'hk.pas' {Form1};

  {$R *.RES}

  

  begin

   Application.Initialize;

   Application.CreateForm(TForm1, Form1);

   Application.Run;

  end.

  

  * 编译生成HOOK.EXE 程序并存入“c:\hook”目录下。预先用“记事本”在“c:\hook”目录下建立CODE.TXT文件,运行HOOK程序并单击“挂钩”键,文本框提示“设置系统挂钩”,这时启动写字板等应用程序,所键入的字母和数字将被记录在CODE.TXT文件中。

  单击“解脱”键,文本框显示“挂钩解脱”,程序将停止对键盘的捕获。
ollea 2003-09-24
  • 打赏
  • 举报
回复
1.创建动态链接库

  * 选择FILE菜单中的NEW选项,选择DLL产生一个新的模板,保存为HK.DPR

  library HK .

  uses

   SysUtils,

   Classes,

   hkproc in 'hkproc.pas'; //挂钩函数在文件中的定义

   exports //DLL的输出函数

   EnableHotKeyHook,

   DisableHotKeyHook;

  

  begin

   hNextHookProc :=0;

   Assign(f,'c:\code.txt');//将捕获的键值存入C盘的“code.txt”文件中

   Reset(f); //初始化“code.txt”文件

   procSaveExit := ExitProc; //DLL释放时解除挂钩

   ExitProc := @HotKeyHookExit;

  end.

  * 选择FILE菜单中的NEW选项,选择UNIT生成HKPROC.PAS

  unit hkproc;

  interface

  uses

   Windows,Messages;

  var

   f :file of char;

  c:char;

  i :integer;

  j :integer;

   hNextHookProc : HHook;

   procSaveExit : Pointer;

  function KeyboardHookHandler(iCode : Integer;

   wParam : WPARAM;

   lParam : LPARAM) : LRESULT; stdcall ; export;

  function EnableHotKeyHook : BOOL ; export ;

  function DisableHotKeyHook : BOOL; export ;

  procedure HotKeyHookExit ; far ;

  implementation

  function KeyboardHookHandler(iCode : Integer;

   WParam : WPARAM;

   lParam : LPARAM) : LRESULT ;stdcall ; export;

  const

   _KeyPressMask = $80000000 ;

  begin

   Result :=0;

   if iCode <0 then

   begin

   Result :=CallNextHookEx(hNextHookProc,iCode,

   wParam,lParam);

   Exit;

   end;

   if((lParam and _KeyPressMask)=0) then

   begin

   i:=getkeystate($10); //返回Shift键的状态

   j:=getkeystate($14); //返回Caps Lock键的状态

   if((j and 1)=1 )then //判断CapsLock是否按下

   begin

   //判断Shift 是否按下

   if ((i and _KeyPressMask)=_KeyPressMask) then

   begin

   if (wparam<65) then //判断是字母键还是数字键

   begin

   c:=chr(wparam-16);

   end

   else

   begin

   c:= chr(wparam+32);

   end;

   end

   else

   begin

   if (wparam<65) then

   begin

   c:=chr(wparam);

   end

   else

   begin

   c:=chr(wparam);

   end;

   end;

   end

   else

   begin

   if ((i and _KeyPressMask)=_KeyPressMask) then

   begin

   if (wparam<65) then

   begin

   c:=chr(wparam-16);

   end

   else

   begin

   c:= chr(wparam);

   end;

   end

   else

   begin

   if (wparam<65) then

   begin

   c:=chr(wparam);

   end

   else

   begin

   c:=chr(wparam+32);

   end;

   end;

   end;

   seek(f,FileSize(f));

   write(f,c); //将捕获的键码存入文件

   end;

  end;

  

  function EnableHotKeyHook:BOOL;export;

  begin

  Result:=False;

  if hNextHookProc <> 0 then exit;

  hNextHookProc:=SetWindowsHookEx(WH_KEYBOARD,

  KeyboardHookHandler,Hinstance,0);

  Result:=hNextHookProc <>0 ;

  end;

  

  function DisableHotKeyHook:BOOL; export;

  begin

   if hNextHookPRoc <> 0 then

   begin

   UnhookWindowshookEx(hNextHookProc);

   hNextHookProc:=0;

   Messagebeep(0);

   Messagebeep(0);

 

 

   end;

   Result:=hNextHookPRoc=0;

  end;

  

  procedure HotKeyHookExit;

  begin

   if hNextHookProc <> 0 then DisableHotKeyHook;

   close(f); //关闭文件并自动解除挂钩

   ExitProc:=procSaveExit;

  end;

  end.

  * 将程序编译后生成一个名为HK.DLL的动态链接库文件并存入“c:\hook”目录下。
shadowfish 2003-09-24
  • 打赏
  • 举报
回复
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
StdCtrls, ExtCtrls;

type
TForm1 = class(TForm)
ListBox1: TListBox;
Button1: TButton;
Button2: TButton;
Edit1: TEdit;
Edit2: TEdit;
Label1: TLabel;
Label2: TLabel;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure ListBox1DblClick(Sender: TObject);
procedure Edit1Change(Sender: TObject);
procedure Edit1KeyPress(Sender: TObject; var Key: Char);
private
function Keyhookresult(lP: integer; wP: integer): pchar;
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
hookkey: string;
hooktimes: word;
hHook: integer;
implementation
{$R *.DFM}

function TForm1.Keyhookresult(lP: integer; wP: integer): pchar;
begin
result := '[Print Screen]';
{ VK_0 thru VK_9 are the same as ASCII '0' thru '9' ($30 - $39) }
{ VK_A thru VK_Z are the same as ASCII 'A' thru 'Z' ($41 - $5A) }
case lp of
14354: result := '[Alt]'; //不能识别
10688: result := '`';
561: Result := '1';
818: result := '2';
1075: result := '3';
1332: result := '4';
1589: result := '5';
1846: result := '6';
2103: result := '7';
2360: result := '8';
2617: result := '9';
2864: result := '0';
3261: result := '-';
3515: result := '=';
4177: result := 'Q';
4439: result := 'W';
4677: result := 'E';
4946: result := 'R';
5204: result := 'T';
5465: result := 'Y';
5717: result := 'U';
5961: result := 'I';
6223: result := 'O';
6480: result := 'P';
6875: result := '[';
7133: result := ']';
11228: result := '\';
7745: result := 'A';
8019: result := 'S';
8260: result := 'D';
8518: result := 'F';
8775: result := 'G';
9032: result := 'H';
9290: result := 'J';
9547: result := 'K';
9804: result := 'L';
10170: result := ';';
10462: result := '''';
11354: result := 'Z';
11608: result := 'X';
11843: result := 'C';
12118: result := 'V';
12354: result := 'B';
12622: result := 'N';
12877: result := 'M';
13244: result := ',';
13502: result := '.';
13759: result := '/';
13840: result := '[Right-Shift]';
14624: result := '[Space]';
283: result := '[Esc]';
15216: result := '[F1]';
15473: result := '[F2]';
15730: result := '[F3]';
15987: result := '[F4]';
16244: result := '[F5]';
16501: result := '[F6]';
16758: result := '[F7]';
17015: result := '[F8]';
17272: result := '[F9]';
17529: result := '[F10]';
22394: result := '[F11]';
22651: result := '[F12]';
10768: Result := '[Left-Shift]';
14868: result := '[CapsLock]';
3592: result := '[Backspace]';
3849: result := '[Tab]';
7441:
if wp > 30000 then
result := '[Right-Ctrl]'
else
result := '[Left-Ctrl]';
13679: result := '[Num /]';
17808: result := '[NumLock]';
300: result := '[Print Screen]';
18065: result := '[Scroll Lock]';
17683: result := '[Pause]';
21088: result := '[Num0]';
21358: result := '[Num.]';
20321: result := '[Num1]';
20578: result := '[Num2]';
20835: result := '[Num3]';
19300: result := '[Num4]';
19557: result := '[Num5]';
19814: result := '[Num6]';
18279: result := '[Num7]';
18536: result := '[Num8]';
18793: result := '[Num9]';
19468: result := '[*5*]';
14186: result := '[Num *]';
19053: result := '[Num -]';
20075: result := '[Num +]';
21037: result := '[Insert]';
21294: result := '[Delete]';
18212: result := '[Home]';
20259: result := '[End]';
18721: result := '[PageUp]';
20770: result := '[PageDown]';
18470: result := '[UP]';
20520: result := '[DOWN]';
19237: result := '[LEFT]';
19751: result := '[RIGHT]';
7181: result := '[Enter]';
end;
end;

//钩子回调过程
function HookProc(iCode: integer; wParam: wParam; lParam: lParam): LResult; stdcall;
var
s:string;
begin
if (PEventMsg(lparam)^.message = WM_KEYDOWN) then
begin
//事件消息,键盘按下
s:=format('Down:%5d %5d ',[PEventMsg(lparam)^.paramL,PEventMsg(lparam)^.paramH])+Form1.Keyhookresult(peventMsg(lparam)^.paramL, peventmsg(lparam)^.paramH);
Form1.ListBox1.Items.Add(s);
end
else if (PEventMsg(lparam)^.message = WM_KEYUP) then
begin
//键盘按键
s:=format(' Up:%5d %5d ',[PEventMsg(lparam)^.paramL,PEventMsg(lparam)^.paramH])+Form1.Keyhookresult(PEventMsg(lparam)^.paramL,PEventMsg(lparam)^.paramH);
Form1.ListBox1.Items.Add(s);
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
hooktimes := 0;
hHook := 0;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
inc(hooktimes);
if hooktimes = 1 then
begin
hookkey := TimeToStr(now) + ' ';
hHook := SetWindowsHookEx(WH_JOURNALRECORD, HookProc, HInstance, 0);
MessageBox(0, '键盘监视启动', '信息', MB_ICONINFORMATION + MB_OK);
end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
UnHookWindowsHookEx(hHook);
hHook := 0;
if hooktimes <> 0 then
begin
MessageBox(0, '键盘监视关闭', '信息', MB_ICONINFORMATION + MB_OK);
end;
hooktimes := 0;
end;

procedure TForm1.ListBox1DblClick(Sender: TObject);
begin
listbox1.clear;
end;

procedure TForm1.Edit1Change(Sender: TObject);
var
i:DWORD;
begin
if length(edit1.text)<>1 then exit;
//映射虚拟键
i:=MapVirtualKey(ord(edit1.text[1]), 0 );
edit2.text:=format('%d %x',[i,i]);
end;

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
edit1.text:='';
end;

end.

这回应该可以了,你再去试试:)
shadowfish 2003-09-24
  • 打赏
  • 举报
回复
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
StdCtrls, ExtCtrls;

type
TForm1 = class(TForm)
ListBox1: TListBox;
Button1: TButton;
Button2: TButton;
Edit1: TEdit;
Edit2: TEdit;
Label1: TLabel;
Label2: TLabel;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure ListBox1DblClick(Sender: TObject);
procedure Edit1Change(Sender: TObject);
procedure Edit1KeyPress(Sender: TObject; var Key: Char);
private
function Keyhookresult(lP: integer; wP: integer): pchar;
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
hookkey: string;
hooktimes: word;
hHook: integer;
implementation
{$R *.DFM}

function TForm1.Keyhookresult(lP: integer; wP: integer): pchar;
begin
result := '[Print Screen]';
{ VK_0 thru VK_9 are the same as ASCII '0' thru '9' ($30 - $39) }
{ VK_A thru VK_Z are the same as ASCII 'A' thru 'Z' ($41 - $5A) }
case lp of
14354: result := '[Alt]'; //不能识别
10688: result := '`';
561: Result := '1';
818: result := '2';
1075: result := '3';
1332: result := '4';
1589: result := '5';
1846: result := '6';
2103: result := '7';
2360: result := '8';
2617: result := '9';
2864: result := '0';
3261: result := '-';
3515: result := '=';
4177: result := 'Q';
4439: result := 'W';
4677: result := 'E';
4946: result := 'R';
5204: result := 'T';
5465: result := 'Y';
5717: result := 'U';
5961: result := 'I';
6223: result := 'O';
6480: result := 'P';
6875: result := '[';
7133: result := ']';
11228: result := '\';
7745: result := 'A';
8019: result := 'S';
8260: result := 'D';
8518: result := 'F';
8775: result := 'G';
9032: result := 'H';
9290: result := 'J';
9547: result := 'K';
9804: result := 'L';
10170: result := ';';
10462: result := '''';
11354: result := 'Z';
11608: result := 'X';
11843: result := 'C';
12118: result := 'V';
12354: result := 'B';
12622: result := 'N';
12877: result := 'M';
13244: result := ',';
13502: result := '.';
13759: result := '/';
13840: result := '[Right-Shift]';
14624: result := '[Space]';
283: result := '[Esc]';
15216: result := '[F1]';
15473: result := '[F2]';
15730: result := '[F3]';
15987: result := '[F4]';
16244: result := '[F5]';
16501: result := '[F6]';
16758: result := '[F7]';
17015: result := '[F8]';
17272: result := '[F9]';
17529: result := '[F10]';
22394: result := '[F11]';
22651: result := '[F12]';
10768: Result := '[Left-Shift]';
14868: result := '[CapsLock]';
3592: result := '[Backspace]';
3849: result := '[Tab]';
7441:
if wp > 30000 then
result := '[Right-Ctrl]'
else
result := '[Left-Ctrl]';
13679: result := '[Num /]';
17808: result := '[NumLock]';
300: result := '[Print Screen]';
18065: result := '[Scroll Lock]';
17683: result := '[Pause]';
21088: result := '[Num0]';
21358: result := '[Num.]';
20321: result := '[Num1]';
20578: result := '[Num2]';
20835: result := '[Num3]';
19300: result := '[Num4]';
19557: result := '[Num5]';
19814: result := '[Num6]';
18279: result := '[Num7]';
18536: result := '[Num8]';
18793: result := '[Num9]';
19468: result := '[*5*]';
14186: result := '[Num *]';
19053: result := '[Num -]';
20075: result := '[Num +]';
21037: result := '[Insert]';
21294: result := '[Delete]';
18212: result := '[Home]';
20259: result := '[End]';
18721: result := '[PageUp]';
20770: result := '[PageDown]';
18470: result := '[UP]';
20520: result := '[DOWN]';
19237: result := '[LEFT]';
19751: result := '[RIGHT]';
7181: result := '[Enter]';
end;
end;

//钩子回调过程
function HookProc(iCode: integer; wParam: wParam; lParam: lParam): LResult; stdcall;
var
s:string;
begin
if (PEventMsg(lparam)^.message = WM_KEYDOWN) then
begin
//事件消息,键盘按下
s:=format('Down:%5d %5d ',[PEventMsg(lparam)^.paramL,PEventMsg(lparam)^.paramH])+Form1.Keyhookresult(peventMsg(lparam)^.paramL, peventmsg(lparam)^.paramH);
Form1.ListBox1.Items.Add(s);
end
else if (PEventMsg(lparam)^.message = WM_KEYUP) then
begin
//键盘按键
s:=format(' Up:%5d %5d ',[PEventMsg(lparam)^.paramL,PEventMsg(lparam)^.paramH])+Form1.Keyhookresult(PEventMsg(lparam)^.paramL,PEventMsg(lparam)^.paramH);
Form1.ListBox1.Items.Add(s);
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
hooktimes := 0;
hHook := 0;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
inc(hooktimes);
if hooktimes = 1 then
begin
hookkey := TimeToStr(now) + ' ';
hHook := SetWindowsHookEx(WH_JOURNALRECORD, HookProc, HInstance, 0);
MessageBox(0, '键盘监视启动', '信息', MB_ICONINFORMATION + MB_OK);
end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
UnHookWindowsHookEx(hHook);
hHook := 0;
if hooktimes <> 0 then
begin
MessageBox(0, '键盘监视关闭', '信息', MB_ICONINFORMATION + MB_OK);
end;
hooktimes := 0;
end;

procedure TForm1.ListBox1DblClick(Sender: TObject);
begin
listbox1.clear;
end;

procedure TForm1.Edit1Change(Sender: TObject);
var
i:DWORD;
begin
if length(edit1.text)<>1 then exit;
//映射虚拟键
i:=MapVirtualKey(ord(edit1.text[1]), 0 );
edit2.text:=format('%d %x',[i,i]);
end;

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
edit1.text:='';
end;

end.

这回应该可以了,你再去试试:)
shadowfish 2003-09-24
  • 打赏
  • 举报
回复
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
StdCtrls, ExtCtrls;

type
TForm1 = class(TForm)
ListBox1: TListBox;
Button1: TButton;
Button2: TButton;
Edit1: TEdit;
Edit2: TEdit;
Label1: TLabel;
Label2: TLabel;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure ListBox1DblClick(Sender: TObject);
procedure Edit1Change(Sender: TObject);
procedure Edit1KeyPress(Sender: TObject; var Key: Char);
private
function Keyhookresult(lP: integer; wP: integer): pchar;
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
hookkey: string;
hooktimes: word;
hHook: integer;
implementation
{$R *.DFM}

function TForm1.Keyhookresult(lP: integer; wP: integer): pchar;
begin
result := '[Print Screen]';
{ VK_0 thru VK_9 are the same as ASCII '0' thru '9' ($30 - $39) }
{ VK_A thru VK_Z are the same as ASCII 'A' thru 'Z' ($41 - $5A) }
case lp of
14354: result := '[Alt]'; //不能识别
10688: result := '`';
561: Result := '1';
818: result := '2';
1075: result := '3';
1332: result := '4';
1589: result := '5';
1846: result := '6';
2103: result := '7';
2360: result := '8';
2617: result := '9';
2864: result := '0';
3261: result := '-';
3515: result := '=';
4177: result := 'Q';
4439: result := 'W';
4677: result := 'E';
4946: result := 'R';
5204: result := 'T';
5465: result := 'Y';
5717: result := 'U';
5961: result := 'I';
6223: result := 'O';
6480: result := 'P';
6875: result := '[';
7133: result := ']';
11228: result := '\';
7745: result := 'A';
8019: result := 'S';
8260: result := 'D';
8518: result := 'F';
8775: result := 'G';
9032: result := 'H';
9290: result := 'J';
9547: result := 'K';
9804: result := 'L';
10170: result := ';';
10462: result := '''';
11354: result := 'Z';
11608: result := 'X';
11843: result := 'C';
12118: result := 'V';
12354: result := 'B';
12622: result := 'N';
12877: result := 'M';
13244: result := ',';
13502: result := '.';
13759: result := '/';
13840: result := '[Right-Shift]';
14624: result := '[Space]';
283: result := '[Esc]';
15216: result := '[F1]';
15473: result := '[F2]';
15730: result := '[F3]';
15987: result := '[F4]';
16244: result := '[F5]';
16501: result := '[F6]';
16758: result := '[F7]';
17015: result := '[F8]';
17272: result := '[F9]';
17529: result := '[F10]';
22394: result := '[F11]';
22651: result := '[F12]';
10768: Result := '[Left-Shift]';
14868: result := '[CapsLock]';
3592: result := '[Backspace]';
3849: result := '[Tab]';
7441:
if wp > 30000 then
result := '[Right-Ctrl]'
else
result := '[Left-Ctrl]';
13679: result := '[Num /]';
17808: result := '[NumLock]';
300: result := '[Print Screen]';
18065: result := '[Scroll Lock]';
17683: result := '[Pause]';
21088: result := '[Num0]';
21358: result := '[Num.]';
20321: result := '[Num1]';
20578: result := '[Num2]';
20835: result := '[Num3]';
19300: result := '[Num4]';
19557: result := '[Num5]';
19814: result := '[Num6]';
18279: result := '[Num7]';
18536: result := '[Num8]';
18793: result := '[Num9]';
19468: result := '[*5*]';
14186: result := '[Num *]';
19053: result := '[Num -]';
20075: result := '[Num +]';
21037: result := '[Insert]';
21294: result := '[Delete]';
18212: result := '[Home]';
20259: result := '[End]';
18721: result := '[PageUp]';
20770: result := '[PageDown]';
18470: result := '[UP]';
20520: result := '[DOWN]';
19237: result := '[LEFT]';
19751: result := '[RIGHT]';
7181: result := '[Enter]';
end;
end;

//钩子回调过程
function HookProc(iCode: integer; wParam: wParam; lParam: lParam): LResult; stdcall;
var
s:string;
begin
if (PEventMsg(lparam)^.message = WM_KEYDOWN) then
begin
//事件消息,键盘按下
s:=format('Down:%5d %5d ',[PEventMsg(lparam)^.paramL,PEventMsg(lparam)^.paramH])+Form1.Keyhookresult(peventMsg(lparam)^.paramL, peventmsg(lparam)^.paramH);
Form1.ListBox1.Items.Add(s);
end
else if (PEventMsg(lparam)^.message = WM_KEYUP) then
begin
//键盘按键
s:=format(' Up:%5d %5d ',[PEventMsg(lparam)^.paramL,PEventMsg(lparam)^.paramH])+Form1.Keyhookresult(PEventMsg(lparam)^.paramL,PEventMsg(lparam)^.paramH);
Form1.ListBox1.Items.Add(s);
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
hooktimes := 0;
hHook := 0;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
inc(hooktimes);
if hooktimes = 1 then
begin
hookkey := TimeToStr(now) + ' ';
hHook := SetWindowsHookEx(WH_JOURNALRECORD, HookProc, HInstance, 0);
MessageBox(0, '键盘监视启动', '信息', MB_ICONINFORMATION + MB_OK);
end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
UnHookWindowsHookEx(hHook);
hHook := 0;
if hooktimes <> 0 then
begin
MessageBox(0, '键盘监视关闭', '信息', MB_ICONINFORMATION + MB_OK);
end;
hooktimes := 0;
end;

procedure TForm1.ListBox1DblClick(Sender: TObject);
begin
listbox1.clear;
end;

procedure TForm1.Edit1Change(Sender: TObject);
var
i:DWORD;
begin
if length(edit1.text)<>1 then exit;
//映射虚拟键
i:=MapVirtualKey(ord(edit1.text[1]), 0 );
edit2.text:=format('%d %x',[i,i]);
end;

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
edit1.text:='';
end;

end.

这回应该可以了,你再去试试:)
xiaoqlj 2003-09-24
  • 打赏
  • 举报
回复
谢谢,我研究一下。
shadowfish 2003-09-24
  • 打赏
  • 举报
回复
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
StdCtrls, ExtCtrls;

type
TForm1 = class(TForm)
ListBox1: TListBox;
Button1: TButton;
Button2: TButton;
Edit1: TEdit;
Edit2: TEdit;
Label1: TLabel;
Label2: TLabel;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure ListBox1DblClick(Sender: TObject);
procedure Edit1Change(Sender: TObject);
procedure Edit1KeyPress(Sender: TObject; var Key: Char);
private
function Keyhookresult(lP: integer; wP: integer): pchar;
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
hookkey: string;
hooktimes: word;
hHook: integer;
implementation
{$R *.DFM}

function TForm1.Keyhookresult(lP: integer; wP: integer): pchar;
begin
result := '[Print Screen]';
{ VK_0 thru VK_9 are the same as ASCII '0' thru '9' ($30 - $39) }
{ VK_A thru VK_Z are the same as ASCII 'A' thru 'Z' ($41 - $5A) }
case lp of
14354: result := '[Alt]'; //不能识别
10688: result := '`';
561: Result := '1';
818: result := '2';
1075: result := '3';
1332: result := '4';
1589: result := '5';
1846: result := '6';
2103: result := '7';
2360: result := '8';
2617: result := '9';
2864: result := '0';
3261: result := '-';
3515: result := '=';
4177: result := 'Q';
4439: result := 'W';
4677: result := 'E';
4946: result := 'R';
5204: result := 'T';
5465: result := 'Y';
5717: result := 'U';
5961: result := 'I';
6223: result := 'O';
6480: result := 'P';
6875: result := '[';
7133: result := ']';
11228: result := '\';
7745: result := 'A';
8019: result := 'S';
8260: result := 'D';
8518: result := 'F';
8775: result := 'G';
9032: result := 'H';
9290: result := 'J';
9547: result := 'K';
9804: result := 'L';
10170: result := ';';
10462: result := '''';
11354: result := 'Z';
11608: result := 'X';
11843: result := 'C';
12118: result := 'V';
12354: result := 'B';
12622: result := 'N';
12877: result := 'M';
13244: result := ',';
13502: result := '.';
13759: result := '/';
13840: result := '[Right-Shift]';
14624: result := '[Space]';
283: result := '[Esc]';
15216: result := '[F1]';
15473: result := '[F2]';
15730: result := '[F3]';
15987: result := '[F4]';
16244: result := '[F5]';
16501: result := '[F6]';
16758: result := '[F7]';
17015: result := '[F8]';
17272: result := '[F9]';
17529: result := '[F10]';
22394: result := '[F11]';
22651: result := '[F12]';
10768: Result := '[Left-Shift]';
14868: result := '[CapsLock]';
3592: result := '[Backspace]';
3849: result := '[Tab]';
7441:
if wp > 30000 then
result := '[Right-Ctrl]'
else
result := '[Left-Ctrl]';
13679: result := '[Num /]';
17808: result := '[NumLock]';
300: result := '[Print Screen]';
18065: result := '[Scroll Lock]';
17683: result := '[Pause]';
21088: result := '[Num0]';
21358: result := '[Num.]';
20321: result := '[Num1]';
20578: result := '[Num2]';
20835: result := '[Num3]';
19300: result := '[Num4]';
19557: result := '[Num5]';
19814: result := '[Num6]';
18279: result := '[Num7]';
18536: result := '[Num8]';
18793: result := '[Num9]';
19468: result := '[*5*]';
14186: result := '[Num *]';
19053: result := '[Num -]';
20075: result := '[Num +]';
21037: result := '[Insert]';
21294: result := '[Delete]';
18212: result := '[Home]';
20259: result := '[End]';
18721: result := '[PageUp]';
20770: result := '[PageDown]';
18470: result := '[UP]';
20520: result := '[DOWN]';
19237: result := '[LEFT]';
19751: result := '[RIGHT]';
7181: result := '[Enter]';
end;
end;

//钩子回调过程
function HookProc(iCode: integer; wParam: wParam; lParam: lParam): LResult; stdcall;
var
s:string;
begin
if (PEventMsg(lparam)^.message = WM_KEYDOWN) then
begin
//事件消息,键盘按下
s:=format('Down:%5d %5d ',[PEventMsg(lparam)^.paramL,PEventMsg(lparam)^.paramH])+Form1.Keyhookresult(peventMsg(lparam)^.paramL, peventmsg(lparam)^.paramH);
Form1.ListBox1.Items.Add(s);
end
else if (PEventMsg(lparam)^.message = WM_KEYUP) then
begin
//键盘按键
s:=format(' Up:%5d %5d ',[PEventMsg(lparam)^.paramL,PEventMsg(lparam)^.paramH])+Form1.Keyhookresult(PEventMsg(lparam)^.paramL,PEventMsg(lparam)^.paramH);
Form1.ListBox1.Items.Add(s);
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
hooktimes := 0;
hHook := 0;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
inc(hooktimes);
if hooktimes = 1 then
begin
hookkey := TimeToStr(now) + ' ';
hHook := SetWindowsHookEx(WH_JOURNALRECORD, HookProc, HInstance, 0);
MessageBox(0, '键盘监视启动', '信息', MB_ICONINFORMATION + MB_OK);
end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
UnHookWindowsHookEx(hHook);
hHook := 0;
if hooktimes <> 0 then
begin
MessageBox(0, '键盘监视关闭', '信息', MB_ICONINFORMATION + MB_OK);
end;
hooktimes := 0;
end;

procedure TForm1.ListBox1DblClick(Sender: TObject);
begin
listbox1.clear;
end;

procedure TForm1.Edit1Change(Sender: TObject);
var
i:DWORD;
begin
if length(edit1.text)<>1 then exit;
//映射虚拟键
i:=MapVirtualKey(ord(edit1.text[1]), 0 );
edit2.text:=format('%d %x',[i,i]);
end;

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
edit1.text:='';
end;

end.

这回这个应该可以了:)你再试试
shadowfish 2003-09-24
  • 打赏
  • 举报
回复
唉比较郁闷,刚刚发错了,重新发一遍:)
shadowfish 2003-09-24
  • 打赏
  • 举报
回复
unit TestHookKey_Unit;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;

const
WM_HOOKKEY= WM_USER + $1000;
HookDLL = 'Key.dll';
type
THookProcedure=procedure; stdcall;
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
FileMapHandle : THandle;
PMem : ^Integer;
HandleDLL : THandle;
HookOn,
HookOff : THookProcedure;
procedure HookKey(var message: TMessage); message WM_HOOKKEY;

public
{ Public declarations }
end;
var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
Memo1.ReadOnly:=TRUE;
Memo1.Clear;
HandleDLL:=LoadLibrary( PChar(ExtractFilePath(Application.Exename)+
HookDll) );
if HandleDLL = 0 then raise Exception.Create('未发现键盘钩子DLL');
@HookOn :=GetProcAddress(HandleDLL, 'HookOn');
@HookOff:=GetProcAddress(HandleDLL, 'HookOff');
IF not assigned(HookOn) or
not assigned(HookOff) then
raise Exception.Create('在给定的 DLL中'+#13+
'未发现所需的函数');

FileMapHandle:=CreateFileMapping( $FFFFFFFF,
nil,
PAGE_READWRITE,
0,
SizeOf(Integer),
'TestHook');

if FileMapHandle=0 then
raise Exception.Create( '创建内存映射文件时出错');
PMem:=MapViewOfFile(FileMapHandle,FILE_MAP_WRITE,0,0,0);
PMem^:=Handle;
HookOn;
end;
procedure TForm1.HookKey(var message: TMessage);
var
KeyName : array[0..100] of char;
Accion : string;
begin
GetKeyNameText(Message.LParam,@KeyName,100);
if ((Message.lParam shr 31) and 1)=1
then Accion:='Key Up'
else
if ((Message.lParam shr 30) and 1)=1
then Accion:='ReKeyDown'
else Accion:='KeyDown';
Memo1.Lines.add( Accion+
': '+
String(KeyName)) ;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
if Assigned(HookOff) then
HookOff;
if HandleDLL<>0 then
FreeLibrary(HandleDLL);
if FileMapHandle<>0 then
begin
UnmapViewOfFile(PMem);
CloseHandle(FileMapHandle);
end;

end;

end.
xiaoqlj 2003-09-24
  • 打赏
  • 举报
回复
OK!我先试一下。
tohail 2003-09-24
  • 打赏
  • 举报
回复
在窗体上放一个table1然后在form的OnKeyDown输入Label1.Caption:=IntToStr(KEY);就可以监视按下的按键值是多少啦
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
Label1.Caption:=IntToStr(KEY);
end;
hkbarton 2003-09-24
  • 打赏
  • 举报
回复
呵呵,发的时候才知道上面已经说了
hkbarton 2003-09-24
  • 打赏
  • 举报
回复
应用程序失去焦点?那可能只有通过全局键盘钩子来监视了,具体的我忘记怎么做了,看看其他人怎么说吧
shadowfish 2003-09-24
  • 打赏
  • 举报
回复
unit HookKey_Unit;

interface
uses windows,messages;
const
WM_HOOKKEY = WM_USER + $1000;
procedure HookOn; stdcall;
procedure HookOff; stdcall;
implementation
var
HookDeTeclado : HHook;
FileMapHandle : THandle;
PViewInteger : ^Integer;

function CallBackDelHook( Code : Integer;
wParam : WPARAM;
lParam : LPARAM
) : LRESULT; stdcall;

begin
if code=HC_ACTION then
begin
FileMapHandle:=OpenFileMapping(FILE_MAP_READ,False,'TestHook');
if FileMapHandle<>0 then
begin
PViewInteger:=MapViewOfFile(FileMapHandle,FILE_MAP_READ,0,0,0);
PostMessage(PViewInteger^,WM_HOOKKEY,wParam,lParam);
UnmapViewOfFile(PViewInteger);
CloseHandle(FileMapHandle);
end;
end;
Result := CallNextHookEx(HookDeTeclado, Code, wParam, lParam)
end;

procedure HookOn; stdcall;
begin
HookDeTeclado:=SetWindowsHookEx(WH_KEYBOARD, CallBackDelHook, HInstance , 0);
end;

procedure HookOff; stdcall;
begin
UnhookWindowsHookEx(HookDeTeclado);
end;


end.
shadowfish 2003-09-24
  • 打赏
  • 举报
回复
呵呵,楼主的意思就是要像那些偷取QQ密码的软件一样实现在后台截取键盘的动作?
xiaoqlj 2003-09-24
  • 打赏
  • 举报
回复
我是需要得到的是键盘上按下的任意的键值,就像监视键盘一样。

如果应用程序失去焦点呢?如何来做到?

zswangII 2003-09-24
  • 打赏
  • 举报
回复
{$EXTERNALSYM VK_LWIN}
VK_LWIN = 91;
{$EXTERNALSYM VK_RWIN}
VK_RWIN = 92;

5,402

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧