﻿#include <APISysConstants.au3>
#include <GUIConstantsEx.au3>
#include <WinAPISys.au3>
#include <WindowsConstants.au3>

; hier muss der DeviceName von eurem Scanner drinstehen
; beim auskommentieren von Zeile 41 wird der DeviceName beim scannen angezeigt.
Global $g_sDevName = '\\?\HID#VID_13BA&PID_0018#6&238b7016&0&0000#{884b96c3-56ef-11d1-bc8c-00a0c91405dd}'

Global $g_hGui = GUICreate('BarcodeScanner', 300, 120)
Global $g_idLabel = GUICtrlCreateLabel('', 5, 5, 290, 30) ; fuer die Ausgabe vom Barcode-Scanner
GUICtrlSetFont(-1, 16)
Global $g_idInput = GUICtrlCreateInput('', 5, 40, 290, 30) ; zum testen ein Input, fuer eine Eingabe von der Tastatur
GUICtrlSetFont(-1, 16)

Local $tRID = DllStructCreate($tagRAWINPUTDEVICE)
$tRID.UsagePage = 0x01
$tRID.Usage = 0x06
$tRID.Flags = $RIDEV_INPUTSINK
$tRID.hTarget = $g_hGui
_WinAPI_RegisterRawInputDevices($tRID)

GUIRegisterMsg($WM_INPUT, 'WM_INPUT')
GUISetState(@SW_SHOW)
GUICtrlSetState($g_idInput, $GUI_FOCUS)

Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE
Exit

Func WM_INPUT($hWnd, $iMsg, $wParam, $lParam)
	#forceref $hWnd, $iMsg, $wParam
	Local Static $sBuffer = ''
	Local $devType, $devName, $tRIM, $tText
	$tRIM = DllStructCreate($tagRAWINPUTHEADER)
	If Not _WinAPI_GetRawInputData($lParam, $tRIM, DllStructGetSize($tRIM), $RID_HEADER) Then Return
	$devType = $tRIM.Type
	$tText = DllStructCreate('wchar data[256]')
	If Not _WinAPI_GetRawInputDeviceInfo($tRIM.hDevice, $tText, 256, $RIDI_DEVICENAME) Then Return
	$devName = $tText.data
;~ 	ConsoleWrite(StringFormat("DevName: '%s'\r\n", $devName))
	If $devName = $g_sDevName Then ; hier wird getestet, ob die Eingabe vom Barcode-Scanner kommt
		If $devType = $RIM_TYPEKEYBOARD Then ; sicherstellen, dass eine Tastatur-Emulation erfolgt
			$tRIM = DllStructCreate($tagRAWINPUTKEYBOARD)
			If _WinAPI_GetRawInputData($lParam, $tRIM, DllStructGetSize($tRIM), $RID_INPUT) Then
				Switch $tRIM.Message
					Case $WM_KEYDOWN
						If $tRIM.VKey = 13 Then ; wenn VKey = 13 (@CR) wird bei meinem Barcode-Scanner als Postfix gesendet (evtl. anpassen)
							GUICtrlSetData($g_idLabel, $sBuffer) ; Buffer im Label anzeigen
							$sBuffer = '' ; Buffer leeeren, fuer naechsten Code
							ConsoleWrite('--- New Code ---' & @CRLF) ; nur zum debuggen
						Else ; alle anderen Zeichen werden in den Buffer uebernommen
							$sBuffer &= Chr($tRIM.VKey)
							ConsoleWrite('Char: ' & Chr($tRIM.VKey) & @CRLF) ; nur zum debuggen
						EndIf
				EndSwitch
				_ClearKeyBuffer() ; die Eingabe vom Barcode-Scanner aus dem Msg-Queue loeschen
			EndIf
		EndIf
	EndIf
	Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_INPUT

Func _ClearKeyBuffer()
	Local Const $tagMSG = 'hwnd hwnd;uint message;wparam wParam;lparam lParam;dword time;long X;long Y;dword lPrivate'
	Local Const $PM_REMOVE = 0x0001
	Local $tMsg = DllStructCreate($tagMSG), $ret
	Do
		$ret = DllCall('user32.dll', 'bool', 'PeekMessage', 'struct*', $tMsg, 'hwnd', $g_hGui, 'uint', $WM_KEYFIRST, 'uint', $WM_KEYLAST, 'uint', $PM_REMOVE)[0]
	Until $tMsg.message = 0x0101 Or $tMsg.message = 0
EndFunc
