#include <Misc.au3>
#include <GUIConstantsEx.au3>

$hwnd = GUICreate("GetKeyInput-Test by Faweyr",300,100)
$button = GUICtrlCreateButton("NoKey",100,35,100,30)

GUISetState()

do
	Sleep(20)
	$msg = GUIGetMsg()
	
	If $msg = $button then 						;falls der Button gedrückt wird!
		GUICtrlSetData($button,"Press Key!")
		$result = GetKeyInput()					;5s warten, gibt Array zurück!
		If not @error Then 						;falls eine Taste gedrückt wurde!
			GUICtrlSetData($button,$result[2])	;den Name der Taste als Buttontext setzen
 			ConsoleWrite("_ispressed-Code:	" & $result[0] & @lf & "HotKeySet-Code:		" & $result[1] & @lf & "Key-Name:		" & $result[2] & @lf)
		Else
			GUICtrlSetData($button,"No Key")
		endif
	endif
until $msg = $GUI_EVENT_CLOSE




; Autor:		Faweyr
; Function:		GetKeyInput($sTime,$sBack)
; Parameter:	$sTime = Time to wait and check for pressed Keys
;				$sBack = the kind of Result
;				0 = Array[3] = ["_ispressed"-Code , "Send"-Code , Name of Key]
;				1 = "_ispressed"-Code
;				2 = "Send"-Code
;				3 = Name of Key
; Includes:		<Misc.au3>
func GetKeyInput($sTime = 5000,$sBack = 0)
	$sTimer = TimerInit()
	$sDll = DllOpen("user32.dll")
	Local $sList[111][3] = [	[08,"{BACKSPACE}","Backspace"],			[09,"{TAB}","Tab"],				[0&"D","{ENTER}","Enter"],			["A"&0,"{LSHIFT}","Left Shift"],	["A"&1,"{RSHIFT}","Right Shift"],										_ 
	[13,"{Pause}","Pause"],		[14,"{CAPSLOCK}","Caps Lock"],			[1&"B","{ESC}","Esc"],			[20,"{SPACE}","Spacebar"],			[21,"{PGUP}","Page Up"],			[22,"{PGDN}","Page Down"],			[23,"{End}","End"], 				_
	[24,"{Home}","Home"],		[25,"{Left}","Left Arrow"],				[26,"{UP}","Up Arrow"],			[27,"{Right}","Right Arrow"],		[28,"{Down}","Down Arrow"],			[12,"{ALT}","Alt"],					[5&"C","{RWIN}","Right Windows"],	_
	[2&"C","{PRINTSCREEN}","Print Screen"],								[2&"D","{INS}","Ins"],			[2&"E","{DELETE}","Del"],			[30,0,"0"],							[31,1,"1"],							[32,2,"2"],							_
	[33,3,"3"],					[34,4,"4"],								[35,5,"5"],						[36,6,"6"],							[37,7,"7"],							[38,8,"8"],							[39,9,"9"],							_
	[41,"a","A"],				[42,"b","B"],							[43,"c","C"],					[44,"d","D"],						[45,"e","E"],						[46,"f","F"],						[47,"g","G"],						_
	[48,"h","H"],				[49,"i","I"],							[4&"A","j","J"],				[4&"B","k","K"],					[4&"C","l","L"],					[4&"D","m","M"],					[4&"E","n","N"],					_
	[4&"F","o","O"],			[50,"p","P"],							[51,"q","Q"],					[52,"r","R"],						[53,"s","S"],						[54,"t","T"],						[55,"u","U"],						_
	[56,"v","V"],				[57,"w","W"],							[58,"x","X"],					[59,"y","Y"],						[5&"A","z","Z"], 					[60,"{NUMPAD0}","Keypad 0"],		[61,"{NUMPAD1}","Keypad 1"],		_	
	[62,"{NUMPAD2}","Keypad 2"],[63,"{NUMPAD3}","Keypad 3"],			[64,"{NUMPAD4}","Keypad 4"],	[65,"{NUMPAD5}","Keypad 5"],		[66,"{NUMPAD6}","Keypad 6"],		[67,"{NUMPAD7}","Keypad 7"],		[68,"{NUMPAD8}","Keypad 8"],		_
	[69,"{NUMPAD9}","Keypad 9"],[6&"A","{NUMPADMULT}","Multiply"],		[6&"B","{NUMPADADD}","Add"],	[6&"C","{NUMPADENTER}","Separator"],[6&"D","{NUMPADSUB}","Subtract"],	[6&"E","{NUMPADDOT}","Decimal"],	[6&"F","{NUMPADDIV}","Divide"], 	_
	[70,"{F1}","F1"],			[71,"{F2}","F2"],						[72,"{F3}","F3"],				[73,"{F4}","F4"],					[74,"{F5}","F5"],					[75,"{F6}","F6"],					[76,"{F8}","F7"],					_
	[77,"{F8}","F8"],			[78,"{F9}","F9"],						[79,"{F10}","F10"],				[7&"A","{F11}","F11"],				[7&"B","{F12}","F12"],				[90,"{NUMLOCK}","Num Lock"],		[91,"{SCROLLLOCK}","Scroll Lock"], 	_
	["A"&2,"{LCTRL}","Left Control"],									["A"&3,"{RCTRL}","Right Control"],									["A"&4,"{LALT}","Left Menu"],		["A"&5,"{RALT}","Right Menu"],		[5&"B","{LWIN}","Left Windows"]		]

	do
		for $i = 0 to UBound($sList)-1 step 1
			If _IsPressed($sList[$i][0], $sDll) then
				DllClose($sDll)
				If $sBack <> 0 then 
					Return $sList[$i][$sBack-1]
				else
					Local $sResult[3] = [$sList[$i][0],$sList[$i][1],$sList[$i][2]]
					Return $sResult
				endif
			endif
		Next
	until $sTime <= TimerDiff($sTimer)
	DllClose($sDll)
	SetError(1)
EndFunc