永发信息网

delphi窗体键盘输入事件

答案:2  悬赏:70  手机版
解决时间 2021-03-12 14:40
  • 提问者网友:王者佥
  • 2021-03-12 09:52
我是新手,刚学delphi,请教大侠们一个问题:
我想在窗体内通过键盘按键来实现一些窗体操作,例如按ESC关闭窗体,按空白键模拟点击窗体内指定按钮组件,在无按钮的对话框按Y进行确定操作,按N进行取消操作,要求输入焦点不在任何组件上都能触发事件。
我在窗体的onkeypress事件定义if key=#27 then Free;但在窗体内按ESC没有关闭窗体,我应该在哪里定义呢?
最佳答案
  • 五星知识达人网友:長槍戰八方
  • 2021-03-12 11:09
给你个例子比如输入完edit1中的密码。点击enter键直接引发button1登入按键edit1的Onkeypass事件 if key=#13 then
begin
key:=#0;
button1.Click;
end; 你试试 窗体名.free
全部回答
  • 1楼网友:骨子里都是戏
  • 2021-03-12 11:55
方法1(keybd_event): procedure tform1.btn2click(sender:tobject); var h:hwnd; begin h:=findwindow('notepad',nil); setforegroundwindow(h); showwindow(h,sw_hide); sendkeys('564565465',false); showwindow(h,sw_show); end; sendkeys是d7光盘的里的一个sndkey32单元的函数。 方法2(sendmessage): procedure sendkeysmsg(focushld:hwnd;ssend:string); var i:integer; ch:byte; begin if focushld=0 then exit; i:=1; while i<=length(ssend) do begin ch:=byte(ssend[i]); if isdbcsleadbyte(ch) then begin inc(i); sendmessage(focushld,wm_ime_char,makeword(byte(ssend[i]),ch),0); end else sendmessage(focushld,wm_ime_char,word(ch),0); inc(i); end; end; procedure tform1.btn1click(sender:tobject); var h:hwnd; begin h:=findwindow('notepad',nil); h:=findwindowex(h,0,'edit',nil); sendkeysmsg(h,'i服了u'); end; 方法3(winio):ps2 only function initializewinio:boolean; stdcall;external'winio.dll' name'initializewinio'; function installwiniodriver(pszwiniodriverpath:pstring;isdemandloaded:boolean=false):boolean; stdcall;external'winio.dll' name'installwiniodriver'; function removewiniodriver:boolean; stdcall;external'winio.dll' name'removewiniodriver'; function getportval(portaddr:word;portval:pdword;bsize:byte):boolean; stdcall;external'winio.dll' name'getportval'; function setportval(portaddr:word;portval:dword;bsize:byte):boolean; stdcall;external'winio.dll' name'setportval'; function getphyslong(physaddr:pbyte;physval:pdword):boolean; stdcall;external'winio.dll' name'getphyslong'; function setphyslong(physaddr:pbyte;physval:dword):boolean; stdcall;external'winio.dll' name'setphyslong'; function mapphystolin(physaddr:pbyte;physsize:dword;physmemhandle:phandle):pbyte; stdcall;external'winio.dll' name'mapphystolin'; function unmapphysicalmemory(physmemhandle:thandle;linaddr:pbyte):boolean; stdcall;external'winio.dll' name'unmapphysicalmemory'; procedure shutdownwinio; stdcall;external'winio.dll' name'shutdownwinio'; procedure kbcwait4ibe;//等待键盘缓冲区为空 var dwval:dword; begin repeat getportval(kbc_key_cmd,@dwval,1); until (dwval and $2)=0; end; procedure mykeypress(vkeycoad:integer); var btscancode:dword; begin btscancode:=mapvirtualkey(vkeycoad,0); kbcwait4ibe; setportval(kbc_key_cmd,$d2,1); kbcwait4ibe; setportval(kbc_key_data,btscancode,1); kbcwait4ibe; setportval(kbc_key_cmd,$d2,1); kbcwait4ibe; setportval(kbc_key_data,(btscancode or $80),1); end; procedure mykeyspress(keysstring:string); var i:integer; begin initializewinio; for i:=1 to strlen(pchar(keysstring)) do begin mykeypress(vkkeyscan(keysstring[i])); end; shutdownwinio; end; procedure tform1.btn1click(sender:tobject); var h:hwnd; begin h:=findwindow('notepad',nil); setforegroundwindow(h); mykeyspress('sdf'); end; 方法4(sendinput): procedure sendkeyinput(keys:string); var i:integer; inputs:array[0..1] of tinput; begin inputs[0].itype:=input_keyboard; with inputs[0].ki do begin wscan:=0; dwflags:=0; end; inputs[1].itype:=input_keyboard; with inputs[1].ki do begin wscan:=0; dwflags:=keyeventf_keyup; end; for i:=1 to length(keys) do begin inputs[0].ki.wvk:=vkkeyscan(keys[i]); inputs[1].ki.wvk:=vkkeyscan(keys[i]); sendinput(2,inputs[0],sizeof(tinput)); end; end; h:=findwindow('notepad',nil); setforegroundwindow(h); sendkeyinput('sdf'); 方法5(wh_journalplayback): 参考 delphi建键盘鼠标动作纪录与回放。 方法6(谁来?)
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯