KeyHit... get id?

BlitzMax Forums/BlitzMax Programming/KeyHit... get id?

Yahfree(Posted 2011) [#1]
Hey, I'm writing a little game in blitzmax, and I was wondering if there was a way to do a "traditional" style input check.

You see, it's a word game, where the player presses keys corresponding to letters floating on the screen.

It seems like it'd be easier to check if a key was pressed, then decide what to do based on what key was pressed.

Versus, doing something like this:
If KeyHit(KEY_A) ...
If KeyHit(KEY_B) ...
If KeyHit(KEY_C) ...
If KeyHit(KEY_D) ...


And doing a million if statements per loop.

Or does it really make a difference?


degac(Posted 2011) [#2]
If you want only alphabetic characters you could do something like this
SuperStrict
Graphics 800,600
Local code:Int


While Not AppTerminate()
	
		Cls
		For Local ind:Int=0 To 255
			If KeyHit(ind)=True
					code=GetChar()
					Exit			
			End If
		Next
		
		DrawText Chr(code),100,100
	
		Flip
Wend

Using GetChar to retrieve ASCII code

If you need to intercept SHIFT/CONTROL/LEFT/UP... you should need to look the file BRL.KEYCODES and make a string array of the returned codes.

Last edited 2011


Amon(Posted 2011) [#3]
I got this from the BlitzMax .Input help file by viewing the source. These are the codes returned by each key.

Const MOUSE_LEFT=1
Const MOUSE_RIGHT=2
Const MOUSE_MIDDLE=3

Const MODIFIER_NONE=0
Const MODIFIER_SHIFT=1			'shift key
Const MODIFIER_CONTROL=2		'ctrl key
Const MODIFIER_OPTION=4			'alt or menu key
Const MODIFIER_SYSTEM=8			'windows or apple key

Const MODIFIER_LMOUSE=16		'reserved by Mark!
Const MODIFIER_RMOUSE=32		'reserved by Mark!
Const MODIFIER_MMOUSE=64		'reserved by Mark!

Const MODIFIER_ALT=MODIFIER_OPTION
Const MODIFIER_MENU=MODIFIER_OPTION
Const MODIFIER_APPLE=MODIFIER_SYSTEM
Const MODIFIER_WINDOWS=MODIFIER_SYSTEM

?MACOS
Const MODIFIER_COMMAND=MODIFIER_APPLE
?Win32
Const MODIFIER_COMMAND=MODIFIER_CONTROL
?Linux
Const MODIFIER_COMMAND=MODIFIER_CONTROL
?

Const KEY_BACKSPACE=8
Const KEY_TAB=9
Const KEY_CLEAR=12
Const KEY_RETURN=13
Const KEY_ENTER=13
Const KEY_ESCAPE=27
Const KEY_SPACE=32
Const KEY_PAGEUP=33
Const KEY_PAGEDOWN=34
Const KEY_END=35
Const KEY_HOME=36

Const KEY_LEFT=37,KEY_UP=38,KEY_RIGHT=39,KEY_DOWN=40

Const KEY_SELECT=41
Const KEY_PRINT=42
Const KEY_EXECUTE=43
Const KEY_SCREEN=44
Const KEY_INSERT=45
Const KEY_DELETE=46

Const KEY_0=48,KEY_1=49,KEY_2=50,KEY_3=51,KEY_4=52
Const KEY_5=53,KEY_6=54,KEY_7=55,KEY_8=56,KEY_9=57
Const KEY_A=65,KEY_B=66,KEY_C=67,KEY_D=68,KEY_E=69
Const KEY_F=70,KEY_G=71,KEY_H=72,KEY_I=73,KEY_J=74
Const KEY_K=75,KEY_L=76,KEY_M=77,KEY_N=78,KEY_O=79
Const KEY_P=80,KEY_Q=81,KEY_R=82,KEY_S=83,KEY_T=84
Const KEY_U=85,KEY_V=86,KEY_W=87,KEY_X=88,KEY_Y=89
Const KEY_Z=90

Const KEY_NUM0=96
Const KEY_NUM1=97
Const KEY_NUM2=98
Const KEY_NUM3=99
Const KEY_NUM4=100
Const KEY_NUM5=101
Const KEY_NUM6=102
Const KEY_NUM7=103
Const KEY_NUM8=104
Const KEY_NUM9=105

Const KEY_NUMMULTIPLY=106
Const KEY_NUMADD=107
Const KEY_NUMSUBTRACT=109
Const KEY_NUMDECIMAL=110
Const KEY_NUMDIVIDE=111

Const KEY_F1=112
Const KEY_F2=113
Const KEY_F3=114
Const KEY_F4=115
Const KEY_F5=116
Const KEY_F6=117
Const KEY_F7=118
Const KEY_F8=119
Const KEY_F9=120
Const KEY_F10=121
Const KEY_F11=122
Const KEY_F12=123

Const KEY_TILDE=192
Const KEY_MINUS=189
Const KEY_EQUALS=187

Const KEY_OPENBRACKET=219
Const KEY_CLOSEBRACKET=221
Const KEY_BACKSLASH=226

Const KEY_SEMICOLON=186
Const KEY_QUOTES=222

Const KEY_COMMA=188
Const KEY_PERIOD=190
Const KEY_SLASH=191

Const KEY_LSHIFT=160
Const KEY_RSHIFT=161
Const KEY_LCONTROL=162
Const KEY_RCONTROL=163
Const KEY_LALT=164
Const KEY_RALT=165
Const KEY_LSYS=91
Const KEY_RSYS=92

Rem
Const KEY_PAUSE=19
Const KEY_CAPSLOCK=20
Const KEY_HELP=47
Const KEY_NUMSLASH=108
Const KEY_START=93
Const KEY_NUMLOCK=144
Const KEY_SCROLL=145
Const KEY_BROWSER_BACK=166
Const KEY_BROWSER_FORWARD=167
Const KEY_BROWSER_REFRESH=168
Const KEY_BROWSER_STOP=169
Const KEY_BROWSER_SEARCH=170
Const KEY_BROWSER_FAVORITES=171
Const KEY_BROWSER_HOME=172
Const KEY_VOLUME_MUTE=173
Const KEY_VOLUME_DOWN=174
Const KEY_VOLUME_UP=175
Const KEY_MEDIA_NEXT_TRACK=176
Const KEY_MEDIA_PREV_TRACK=177
Const KEY_MEDIA_STOP=178
Const KEY_MEDIA_PLAY_PAUSE=179
Const KEY_LAUNCH_MAIL=180
Const KEY_LAUNCH_MEDIA_SELECT=181
Const KEY_LAUNCH_APP1=182
Const KEY_LAUNCH_APP2=183
End Rem


What you could do is check for a range of keys using a an If statement or Select/Case Statement.

Example, Psuedo Code:

Need a Global or Local to store what GetChar returns. It stores what the next keypress is when it is pressed. Setup some kind of wait timer to wait until the next key is pressed.

If global charachter is not = to 0
process Select Case loop and check if the charachter returned by GetChar() is > or < e.g in the case of the numbers above the keyboard letter " 0 to 57" the select case statement will check if GetChar returns any of those values >=0 and <= 57. If it is output the corresponding key with drawtext or whatever you use to display the text/ whatever your doing in code then process the next character.

From the BMax Docs:


Function GetChar()
Returns The character code of the next character.
Description Get next character
Information As the user hits keys on the keyboard, BlitzMax records the character codes of these keystrokes into an internal 'character queue'.
GetChar removes the next character code from this queue and returns it the application.

If the character queue is empty, 0 is returned.




Volker(Posted 2011) [#4]
I'm not sure if I got the point, but take a look here:
http://blitzbasic.com/Community/posts.php?topic=83278#939746