Hi Leute,
bin gerade dabei eine Funktion zur systemweiten Registrierung eines ScanEvents (Barcode) zu erstellen. Der HandScanner wird an die PS2 KeyBoard Schnittstelle angeschlossen und die Messagefolge ist mir auch mit Start- und Endkennung bekannt. Mein erster Versuch eine solche Testvariante zu schreiben gelang mir auch direkt. Mein Problem ist nun das in meiner Entwicklungsvariante (=Testvariante) die Messagefolge korrekt über meinen Keyboard Hook ermittle, egal ob die Anwendung nun den Focus besitzt oder nicht.
Es könnte ja so einfach sein, nun wollte ich es in eine etwas aufwendigere Anwendung einbauen (schließlich funktionierte in den Tests alles). Jetzt habe ich folgenden Effekt: hat meine Anwendung den Focus funktioniert alles normal, hat sie den Focus nicht (geht nichts

; ne quatsch natürlich funzt mein Hook, ne aber im Gegensatz zum fokusiertem Zustand erhalte ich jetzt jede Message 2mal, d.h. das erste Erkennungszeichen kommt zweimal, das zweite auch, im Prinzip kommt jede Messag direkt zweimal hintereinander; diesen Unterschied bzw. Effekt hatte ich in meiner Testanwendung nicht da wurde keine Message zuviel gesendet.
--->>> ist TMsg.time in solch einem zusammenhang ein Selektionskriterium ?? (100%ig) gibts überhaupt eins ?
das Problem mit den doppelten Nachrichten ist ja nicht ganz so trivial, schließlich können innerhalb der Nutzdaten durchaus gleiche Werte auf einander folgen.
Ich hoffe jemand hat ne Idee
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240:
| unit Unit_KeyboardHook;
interface
uses Windows, SysUtils, Messages;
const KbHook_TCIKeyboardMsg : string = 'TCIKeyboardMsg'; KbHook_DLLPath : string = 'TCI_KeyBoardHook.dll'; KbHook_WM_Hook_KEYBOARD_MSG : Cardinal = INVALID_HANDLE_VALUE; KbHook_StartErkennungsChar1 : Byte = {$IFDEF TESTMOUDS} 65;{$ELSE} 17;{$ENDIF} KbHook_StartErkennungsChar2 : Byte = {$IFDEF TESTMOUDS} 65;{$ELSE} 66;{$ENDIF} KbHook_EndErkennungsChar1 : Byte = {$IFDEF TESTMOUDS} 65;{$ELSE} 17;{$ENDIF} KbHook_EndErkennungsChar2 : Byte = {$IFDEF TESTMOUDS} 65;{$ELSE} 69;{$ENDIF} KbHook_BarcodeLen : Byte = {$IFDEF TESTMOUDS} 2;{$ELSE} 14;{$ENDIF} KbHook_TickLimit : Word = {$IFDEF TESTMOUDS} 2000;{$ELSE}100;{$ENDIF}
type
TInstallKbHook = Function ( hWindow : cardinal; Broadcasted: Boolean ) : boolean; stdcall; TUninstallKbHook = Function : boolean; stdcall;
Function KbHook_InitBarcodeHook ( hApplication : THandle; Broadcasted : Boolean ): Boolean; Function KbHook_PeekBarcodeA ( Msg : TMsg; var ABarCode: string ): Boolean; Function KbHook_PeekBarcodeB ( Msg : TMsg; var ABarCode: string ): Boolean; Function KbHook_IsHandled ( Msg: TMsg ): Boolean; Procedure KbHook_CheckBarcodeSequence ( var counter: Byte; var Ticker : cardinal; var ABarcode: string; Msg: TMsg );
implementation
var IKeyBoardHook : TInstallKbHook = NIL; UKeyBoardHook : TUninstallKbHook = NIL; HLibrary : THandle = INVALID_HANDLE_VALUE; counter1 : Byte = 0; counter2 : Byte = 0; counter3 : Byte = 0; Barcode : string = ''; Barcode3 : string = ''; TickCount1 : Cardinal = 0; TickCount2 : Cardinal = 0; TickCount3 : Cardinal = 0; MsgTime : Cardinal = 0; MsgWParam : Integer = 0;
Function KbHook_InitBarcodeHook ( hApplication : THandle; Broadcasted : Boolean ): Boolean; begin if FileExists(KbHook_DLLPath) then begin HLibrary := LoadLibrary(PChar(KbHook_DLLPath)); if not ( HLibrary = INVALID_HANDLE_VALUE OR 0 ) then begin IKeyBoardHook := GetProcAddress(HLibrary, 'InstallKeyboardHook'); UKeyBoardHook := GetProcAddress(HLibrary, 'UninstallKeyboardHook'); if IKeyBoardHook (hApplication,Broadcasted) then KbHook_WM_Hook_KEYBOARD_MSG := GetProp(hApplication,PChar(KbHook_TCIKeyboardMsg)); end; end; result := KbHook_WM_Hook_KEYBOARD_MSG <> INVALID_HANDLE_VALUE; end;
Procedure KbHook_CheckBarcodeSequence ( var counter : Byte; var Ticker : cardinal; var ABarcode: string; Msg: TMsg ); begin if counter = 0 then begin if Msg.wParam = KbHook_StartErkennungsChar1 then begin ABarcode := ''; counter := 1; Ticker := GetTickCount; end; end else if counter = 1 then begin if Msg.wParam = KbHook_StartErkennungsChar2 then begin if (Ticker + KbHook_TickLimit) >= GetTickCount then begin inc(counter); Ticker := GetTickCount; end else counter := 0 end else counter := 0 end else if counter in [2..(KbHook_BarcodeLen+1)] then begin if Msg.wParam in [48..57] then begin if (Ticker + KbHook_TickLimit) >= GetTickCount then begin ABarcode := ABarcode + Char(Msg.wParam); inc(counter); Ticker := GetTickCount; end else counter := 0; end else counter := 0 end else if counter = (2+KbHook_BarcodeLen) then begin if Msg.wParam = KbHook_EndErkennungsChar1 then begin if (Ticker + KbHook_TickLimit) >= GetTickCount then begin inc(counter); Ticker := GetTickCount; end else counter := 0; end else counter := 0; end else if counter = (3+KbHook_BarcodeLen) then begin if Msg.wParam = KbHook_EndErkennungsChar2 then begin if (Ticker + KbHook_TickLimit) >= GetTickCount then begin inc(counter); end else counter:=0; end else counter := 0; end; end;
Function KbHook_IsHandled ( Msg: TMsg ): Boolean; var buf : string; begin result := false; case Msg.message of wm_keydown: begin KbHook_CheckBarcodeSequence( counter2, TickCount2, buf, Msg ); result := counter2 > 1; end; end; end;
Function KbHook_PeekBarcodeA ( Msg : TMsg; var ABarCode: string ): Boolean; begin result := false; if (Msg.message = KbHook_WM_Hook_KEYBOARD_MSG) AND (Msg.lParam and $80000000 = 0) then begin
KbHook_CheckBarcodeSequence( counter1, TickCount1, Barcode, Msg );
result := (4+KbHook_BarcodeLen) = counter1; if result then begin ABarcode := Barcode; counter1 := 0; end; end; end;
Function KbHook_PeekBarcodeB ( Msg : TMsg; var ABarCode: string ): Boolean; begin result := false; if (Msg.message = KbHook_WM_Hook_KEYBOARD_MSG) AND (Msg.lParam and $80000000 = 0) then begin
if (MsgTime <> Msg.time) OR (MsgWParam <> Msg.wParam) then begin KbHook_CheckBarcodeSequence( counter3, TickCount3, Barcode3, Msg ); MsgTime := Msg.time; MsgWParam := Msg.wParam; end;
result := (4+KbHook_BarcodeLen) = counter3; if result then begin ABarcode := Barcode3; counter3 := 0; end; if counter3 = 0 then begin MsgTime := 0; MsgWParam := 0; end; end; end;
INITIALIZATION
FINALIZATION begin if not ( HLibrary = INVALID_HANDLE_VALUE OR 0 ) then begin UKeyBoardHook; FreeLibrary(HLibrary); end; end;
end. |