WinIo 键盘记录 为什么鼠标动也会有数据(200)

  • 主题发起人 主题发起人 kingswww
  • 开始时间 开始时间
K

kingswww

Unregistered / Unconfirmed
GUEST, unregistred user!
代码:
我网上找了段代码 自己编译成程序的时候 为什么动鼠标的时候也会出字 各位能帮我看看吗? 我鼠标和键盘都是PS/2接口的 后来换了个USB鼠标就不出这个问题unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, IdBaseComponent, IdThreadComponent;type TForm1 = class(TForm) Button1: TButton; Button2: TButton; Memo1: TMemo; IdThreadComponent1: TIdThreadComponent; procedure FormCreate(Sender: TObject); procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure FormClose(Sender: TObject; var Action: TCloseAction); procedure IdThreadComponent1Run(Sender: TIdCustomThreadComponent); private { Private declarations } public { Public declarations } end; constKeyMask = $80000000;var Form1: TForm1; str: array[0..12] of Char; keyi: Integer;implementation{$R *.dfm}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($64,@dwVal,1); until (dwVal and $2)=0;end;function MyKeyRead:Integer;var btScancode:DWord;begin KBCWait4IBE; GetPortVal($60,@btScancode,1); KBCWait4IBE; Result := MapVirtualKey(btScancode,3);end;procedure TForm1.Button1Click(Sender: TObject);begin IdThreadComponent1.Start; Button1.Enabled := False; Button2.Enabled := True;end;procedure TForm1.Button2Click(Sender: TObject);begin IdThreadComponent1.Stop; Button1.Enabled := True; Button2.Enabled := False;end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);beginShutdownWinIo;end;procedure TForm1.FormCreate(Sender: TObject);begin if InitializeWinIo=False then begin Messagebox(handle,'初始化失败!','提示',MB_OK+MB_IconError) end;end;procedure TForm1.IdThreadComponent1Run(Sender: TIdCustomThreadComponent);varch: Char; i :Integer;begin i :=MyKeyRead; if i = keyi then begin Sleep(50); keyi := 0; end else if (i < 256) and (i > 0) then begin ch:=Chr(i); Memo1.Text := Memo1.Text + ' '+ch; keyi := i; end; Sleep(20);end;end.我找了另外一个procedure KBCWait4IBE; //等待键盘缓冲区为空var dwVal:DWord;begin repeat GetPortVal($64,@dwVal,1); until dwVal = $14;end;就好了 但是程序老是出问题 这个里面的 dwVal = $14; 和 (dwVal and $2)=0; 代表什么意思啊
 
搞清楚 $60,$64 是什么意思,就自然明白了。
 
我已经知道了$60,$64 是PS/2键盘端口 但是还是不知道为什么
 
仔细留意你问题的第一句话。
 
function MyKeyRead:Integer;var btScancode:DWord;begin KBCWait4IBE; GetPortVal($60,@btScancode,1); KBCWait4IBE; Result := MapVirtualKey(btScancode,3);end;改为function MyKeyRead:Integer;var btScancode:DWord;begin KBCWait4OBF; GetPortVal($60,@btScancode,1); Result := MapVirtualKey(btScancode,3);end;KBCWait4OBF()函数定义为:procedure KBCWait4OBF();const OBFBIT = $01;beginasm @Loop: mov al,$00 mov dx,$64 in al,dx test al,OBFBIT jz @Loopend;end;这样就可以了,如果想了解Keyboard从硬件到软件处理的流程,可以看我的BLOG:http://zhaolw1984.blog.163.com/blog/static/5164438520096189948641/
 
后退
顶部