Hello folks,
unfortunately, I have an inexpensive scanner, the Netum NT-L8, which can only send data via the keyboard.
However, I have now found a good solution using a keyboard hook.
What do you think? Is this a good solution or could it cause problems?
What I still don't like, for example, is that the keystrokes are not transmitted correctly.
For example, a URL
https://www.. is transmitted as HTTPS:77WWW.
What could be the reason for this?
Code: Select all
Global G_BarCode.s
Enumeration #PB_Event_FirstCustomValue
#Event_NewBarCode
EndEnumeration
Procedure.l KeyboardHook_Callback( nCode, wParam, *p.KBDLLHOOKSTRUCT )
Static n = 0, BarCode.s
If nCode = #HC_ACTION
If wParam = #WM_KEYDOWN Or wParam = #WM_SYSKEYDOWN
Select *p\vkCode
Case 32 To 123
n + 1
BarCode + Chr(*p\vkCode)
If n > 255
BarCode = ""
n = 0
EndIf
Case 13
G_BarCode = BarCode
n = 0
BarCode = ""
; Debug G_BarCode
PostEvent(#Event_NewBarCode)
EndSelect
EndIf
EndIf
ProcedureReturn CallNextHookEx_(0, nCode, wParam, *p) ; Eingabe zulassen
; ProcedureReturn 1 ; Eingabe verhindern
EndProcedure
Procedure.l SetKeyboardHook ( myKeyboardHook.l, enable.b = #True)
Protected LibID, Result, FuncId, hInstance
hInstance = GetModuleHandle_(#Null)
LibID = OpenLibrary(#PB_Any, "user32.dll" )
If LibID
If enable
FuncId = GetFunction(LibID, "SetWindowsHookExA")
If FuncId
;Result = SetWindowsHookEx_( #WH_KEYBOARD_LL, myKeyboardHook, hInstance, 0)
Result = CallFunctionFast( FuncId, #WH_KEYBOARD_LL, myKeyboardHook, hInstance, #Null )
EndIf
Else
If myKeyboardHook
FuncId = GetFunction(LibID, "UnhookWindowsHookEx")
If FuncId
Result = CallFunctionFast (FuncId, myKeyboardHook)
EndIf
EndIf
EndIf
CloseLibrary(LibID)
EndIf
ProcedureReturn Result
EndProcedure
myHook = SetKeyboardHook(@KeyboardHook_Callback(), #True)
OpenWindow(0, 0, 0, 400, 400, "BarCode Scanner", #PB_Window_SystemMenu)
CanvasGadget(0, 0, 0, 400, 400, #PB_Canvas_Keyboard)
SetActiveGadget(0)
Repeat
Event = WaitWindowEvent()
If Event = #Event_NewBarCode
Debug G_BarCode
;
; ElseIf Event = #PB_Event_Gadget
; If EventType() = #PB_EventType_Input
; c = GetGadgetAttribute(0, #PB_Canvas_Input)
; ;Debug Str(n) + " " + Chr(c) + " " + Str(c)
; n + 1
; EndIf
;
EndIf
Until Event = #PB_Event_CloseWindow