键盘钩子获取后如何判断返回?
sgjie 2021-05-25 09:54:08 现在系统经常被其他程序提前判断给处理掉(比如扫码支付被提前截取处理),于是想自己也写一个钩子先判断。
function keyHookProc(nCode: Integer; LWParam: WPARAM; LLParam: LPARAM): LRESULT;//调用键盘钩子,屏蔽功能键
var
p: LPKBDLLHOOKSTRUCT;
y: integer;
fFlag:boolean;
KeyName: array[0..100] of char;
Kbs: TKeyboardState;
WChar: array[0..1] of Char;
lpmyshare:pmyshare;
hfocus:hwnd;
begin
fFlag:=false;
if nCode < 0 then
begin
Result:= CallNextHookEx(hHk, nCode, LWParam, LLParam);
Exit;
end
else
begin
y := 1;
case LWParam of
WM_KEYDOWN://, WM_SYSKEYDOWN, WM_KEYUP,WM_SYSKEYUP:
begin
p:= LPKBDLLHOOKSTRUCT(LLParam);
BarCodes_usb.VirtKey:=p.vkCode;
BarCodes_usb.scanCode:=p.scanCode;
GetKeyNameText(BarCodes_usb.scanCode, @KeyName, 100);
BarCodes_usb.KeyName:=KeyName;
GetKeyboardState(Kbs);
// 转换成字符
if ToAscii(BarCodes_usb.VirtKey,BarCodes_usb.scanCode, Kbs, WChar, 0) = 1 then
begin
// BarCodes_usb.Ascll = uKey;
BarCodes_usb.Chr :=WChar[0];// Convert.ToChar(uKey);
end;
// showmessage(BarCodes_usb.Chr);
if MilliSecondsBetween(now,BarCodes_usb.Time)> 50 then
begin
strBarCode:=BarCodes_usb.Chr;
end
else
begin
//如果是回车
if (p.vkCode = VK_RETURN) and (length(strBarCode)>3) then
begin
BarCodes_usb.BarCode := strBarCode;
BarCodes_usb.IsValid := true;
end;
strBarCode :=strBarCode+BarCodes_usb.Chr;
end;
BarCodes_usb.Time := Now;
//激活事件
FMMain.show_label;
BarCodes_usb.IsValid := false;
if (p.vkCode = VK_RETURN) then strBarCode :='';
if fFlag then y:=0;
end;
end;
if y=1 then
Result:=1 //如果为WIN功能键则屏蔽
else
Result:= 0;//CallNextHookEx(hHk, nCode, LWParam, LLParam); //其他键放下一个钩子
end
end;
现在问题是这个钩子可以判断输入的字符串,但如何才能做到把这个字符串再根据需要是否回写呢?
比如输入 12345 系统不处理,原来的输入框显示12345
输入123456 钩子就处理,不显示到输入框那?
别的程序就是这么实现的。输入一定的规则就不显示,否则和正常一样。