键盘钩子获取后如何判断返回?

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 钩子就处理,不显示到输入框那?
别的程序就是这么实现的。输入一定的规则就不显示,否则和正常一样。

...全文
2632 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
tanqth 2021-05-26
  • 打赏
  • 举报
回复
你这里,为什么要去返回 0 呢??反到把需要的CallNextHookEx给注释了。 Result:= 0;//CallNextHookEx(hHk, nCode, LWParam, LLParam); //其他键放下一个钩子 正确的方式应该是需要调用CallNextHookEx的。 比如输入 12345 系统不处理,原来的输入框显示12345,这时候,你的程序里就需要调用CallNextHookEx,让钩子链中的下一个使用。 输入123456 钩子就处理,因为是你需要的,那么就可以不让钩子链中的下一个使用,当然其实你也可以让钩子链中的下一个使用。

1,183

社区成员

发帖
与我相关
我的任务
社区描述
Delphi Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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