Code archives/User Input/GetKey()

This code has been declared by its author to be Public Domain code.

Download source code

GetKey() by jkrankie2013
Because GetChar() returns the ascii code rather than the key scan code, i wrote this.

GetKey() returns the scan code for the last key press, useful for when you want users to be able to change key setting without having to pause execution with waitkey (on which this function is based).

Also added a function that returns a string for most scancodes.
Function GetKey:Int()
	Local thiskey:Int = 0
	For Local i:Int = 1 To 255
		If KeyHit(i)
		thiskey = i
		Exit
		EndIf
	Next
	Return thiskey
End Function

Function getKeyCodeString:String(inNum:Int)

If inNum=8  Then Return "Backspace"
If inNum=9  Then Return "Tab"
If inNum=12 Then Return "Clear"
If inNum=13 Then Return "Return"
If inNum=19 Then Return "Pause"
If inNum=20 Then Return "Caps Lock"
If inNum=32 Then Return "Space"
If inNum=33 Then Return "Page Up"
If inNum=34 Then Return "Page Down"
If inNum = 35 Then Return "End"
If inNum=36 Then Return "Home"
If inNum=37 Then Return "Left"
If inNum=38 Then Return "Up"
If inNum=39 Then Return "Right"
If inNum=40 Then Return "Down"
If inNum=41 Then Return "Select"
If inNum=42 Then Return "'print"
If inNum=43 Then Return "Execute"
If inNum=44 Then Return "Screen"
If inNum=45 Then Return "Insert"
If inNum=46 Then Return "Delete"
If inNum=47 Then Return "Help"
If inNum=48 Then Return "0"
If inNum=49 Then Return "1"
If inNum=50 Then Return "2"
If inNum=51 Then Return "3"
If inNum=52 Then Return "4"
If inNum=53 Then Return "5"
If inNum=54 Then Return "6"
If inNum=55 Then Return "7"
If inNum=56 Then Return "8"
If inNum=57 Then Return "9"
If inNum=65 Then Return "A"
If inNum=66 Then Return "B"
If inNum=67 Then Return "C"
If inNum=68 Then Return "D"
If inNum=69 Then Return "E"
If inNum=70 Then Return "F"
If inNum=71 Then Return "G"
If inNum=72 Then Return "H"
If inNum=73 Then Return "I"
If inNum=74 Then Return "J"
If inNum=75 Then Return "K"
If inNum=76 Then Return "L"
If inNum=77 Then Return "M"
If inNum=78 Then Return "N"
If inNum=79 Then Return "O"
If inNum=80 Then Return "P"
If inNum=81 Then Return "Q"
If inNum=82 Then Return "R"
If inNum=83 Then Return "S"
If inNum=84 Then Return "T"
If inNum=85 Then Return "U"
If inNum=86 Then Return "V"
If inNum=87 Then Return "W"
If inNum=88 Then Return "X"
If inNum=89 Then Return "Y"
If inNum=90 Then Return "Z"
If inNum=96 Then Return "Numpad 0"
If inNum=97 Then Return "Numpad 1"
If inNum=98 Then Return "Numpad 2"
If inNum=99 Then Return "Numpad 3"
If inNum=100 Then Return "Numpad 4"
If inNum=101 Then Return "Numpad 5"
If inNum=102 Then Return "Numpad 6"
If inNum=103 Then Return "Numpad 7"
If inNum=104 Then Return "Numpad 8"
If inNum=105 Then Return "Numpad 9"
If inNum=106 Then Return "Numpad *"
If inNum=107 Then Return "Numpad +"
If inNum=109 Then Return "Numpad -"
If inNum=110 Then Return "Numpad ."
If inNum=111 Then Return "Numpad /"
If inNum=112 Then Return "F1"
If inNum=113 Then Return "F2"
If inNum=114 Then Return "F3"
If inNum=115 Then Return "F4"
If inNum=116 Then Return "F5"
If inNum=117 Then Return "F6"
If inNum=118 Then Return "F7"
If inNum=119 Then Return "F8"
If inNum=120 Then Return "F9"
If inNum=121 Then Return "F10"
If inNum=122 Then Return "F11"
If inNum=123 Then Return "F12"
If inNum=144 Then Return "Num Lock"
If inNum=145 Then Return "Scroll Lock"
If inNum=160 Then Return "Shift (Left)"
If inNum=161 Then Return "Shift (Right)"
If inNum=162 Then Return "Control (Left)"
If inNum=163 Then Return "Control (Right)"
If inNum=164 Then Return "Alt key (Left)"
If inNum=165 Then Return "Alt key (Right)"
If inNum=192 Then Return "Tilde"
If inNum=107 Then Return "Minus"
If inNum=109 Then Return "Equals"
If inNum=219 Then Return "Bracket (Open)"
If inNum=221 Then Return "Bracket (Close)"
If inNum=226 Then Return "Backslash"
If inNum=186 Then Return "Semi-colon"
If inNum=222 Then Return "Quote"
If inNum=188 Then Return "Comma"
If inNum=190 Then Return "Period"		
If inNum=191 Then Return "Slash"

End Function

Comments

Brucey2013
KeyHit() can be potentially expensive, since it's also calling PollSystem

Ideally you would have direct access to the private keyHits[] instead…


dna2013
Hey.

I converted this to B3D


While Not KeyDown(1)
	If GetKey()<>0	Then Print getKeyCodeString$( GetKey())
Wend
WaitKey:End


Function GetKey()
	Local thiskey = 0
	Local I
	For I = 1 To 255
		If KeyHit(I)
			thiskey = I
			Exit
		EndIf
	Next
	Return thiskey
End Function


and am having trouble getting it to return anything. Where am I going wrong?


jkrankie2013
But.. you don't have access to the private keyHits array, not outside of changing the module code anyhow..

This isn't really meant to be used in a time sensitive loop or anything, just so i didn't have to halt execution if i used waitkey() when users were reconfiguring their own controls.

@dna, B3d already has this function: http://www.blitzbasic.com/b3ddocs/command.php?name=GetKey&ref=2d_cat usage is likely different (i don't know blitz3d!)

Cheers
Charlie


Addi2013
Hey, here is an other function that allows you to check if any key was hitted (without stopping the programm)
Function KeyHitAny()
     For i = 0 To 255
          If KeyHit(i) Then FlushKeys:Return True
     Next
End Function



virtlands2013
Nice stuff,....
I think that the getKeyCodeString function (from Post 1), could have been
made faster by pre-assembling an array with all the description strings,
(that is, read from DATA statements and stored into an array ahead of time.)

DIM KeyCodeString$(200)

instead of...
Function getKeyCodeString:String(inNum:Int)


matibee2013
VirtLands; Why bother with data statements and pre-assembly when you can do this..
Function getKeyCodeString:String(inNum:Int)
	Local retcode$[] = [ 	"" , "" , "" , "" , "" , "" , "" , "" , "Backspace" , "Tab" , "" , "" , ..
						"Clear" , "Return" , "" , "" , "" , "" , "" , "Pause" , "Caps Lock" , "" , "" , "" , "" , ..
						"" , "" , "" , "" , "" , "" , "" , "Space" , "Page Up" , "Page Down" , "End" , "Home" , "Left" , "Up" , ..
						"Right" , "Down" , "Select" , "'print", "Execute", "Screen", "Insert", "Delete", "Help", ..
						"0" , "1" , "2" , "3" , "4" , "5" , "6" , "7" , "8" , "9" , "" , "" , "" , "" , "" , "" , "" , ..
						"A" , "B" , "C" , "D" , "E" , "F" , "G" , "H" , "I" , "J" , "K" , "L" , "M" , "N" , "O" , "P" , "Q" , "R" , "S" , ..
						"T" , "U" , "V" , "W" , "X" , "Y" , "Z" , ..
						"" , "" , "" , "" , "" , ..
						"Numpad 0" , "Numpad 1" , "Numpad 2" , "Numpad 3" , "Numpad 4" , "Numpad 5" , ..
						"Numpad 6" , "Numpad 7" , "Numpad 8" , "Numpad 9" , "Numpad *" , "Numpad +" , "" , ..
						"Numpad -" , "Numpad ." , "Numpad /" , ..
						"F1" , "F2" , "F3" , "F4" , "F5" , "F6" , "F7" , "F8" , "F9" , "F10" , "F11" , "F12" , ..
						"" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "Num Lock" , ..
						"Scroll Lock" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , ..
						"Shift (Left)" , "Shift (Right)" , "Control (Left)" , "Control (Right)" , "Alt key (Left)" , ..
						"Alt key (Right)" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , ..
						"Semi-colon" , "" , "Comma" , "" , "Period" , "Slash" ]
					
	Return retcode$[inNum]
End Function 

but, speaking as someone that's tried working with arrays like the one above, I've a feeling jkrankie's code is formatted for ease of maintenance. Besides, it's not something that has to be fast.


virtlands2013
Great point Matibee, also, I don't think you can initialize
String arrays that way in Blitz3D.


jkrankie2013
My code is, as Matibee points out, meant to be maintainable, and not for use in speed critical loops. I used it in a high score entry screen, for example.

Cheers
Charlie


Code Archives Forum