similar to b3d getkey()

BlitzMax Forums/BlitzMax Programming/similar to b3d getkey()

Spacechimp(Posted 2009) [#1]
I am trying to write a function that is similar to b3d getkey().

I am doing this by getting the keypress and pulling that corresponding number out of an array. I expected the keypress to sync up with a familiar set of scancodes (b3d) but it doesn't. For instance, when I press the "1" key on my keyboard, my function is returning 49. I could certainly manually poll all my keys, and then re-write the array based on what number I am getting per key pressed. However, I am concerned that my keyboard may return differnt interger values per keypress than other peoples keyboards? Is this correct?




Graphics 640,480
 

 
'Command Console
'---------------
Global mychar$
Global mykey%
 
Global vcharacter$[]=[..
"","","1","2","3","4","5","6","7","8","9","0",.. 
"","","","","Q","W","E","R","T",.. 
"Y","U","I","O","P","[","]","",.. 
"","A","S","D","F","G","H","J","K","L",.. 
";" , "``","`","","\",.. 
"Z","X","C","V","B","N","M",",",".","/",.. 
"","*",""," ","",.. 
"","","","","","","","","","",.. 
"","","7","8","9","-","4",.. 
"5","6","+","1","2","3","0",.. 
".","/","","","","","","","","","","","","","","","","","","",""]
 


While Not KeyHit(KEY_ESCAPE)



	mykey% = GetKeyHit%()
	 
	mychar$ = vcharacter$[mykey%]
	
	 
	DrawText "Hit keys and see what happens in the debug output",0,0

	 
	 
	If mykey% <> 0 Then
	Print mychar$ +" = " +mykey% +" ?"
	EndIf


 
Flip
Wend





Function GetKeyHit%()  
 
	For Local i% = 0 To 255
 
		If KeyHit(i) 
			Return i
		EndIf 
 
	Next 
 
End Function



teamonkey posts something similar to what I am going for here. but I can not understand everything that is going on there. Also, periods and some punctuation do not work.
Can anyone give me advice on how to more successfully emulate blitz3d's getkey() command?

Thanks!


Perturbatio(Posted 2009) [#2]
GetChar()?


Spacechimp(Posted 2009) [#3]
yeah, getchar() works. it is the is almost the same thing that I wrote above.



anyways, below is the scancodes (mostly complete) needed for the code above. Someone let me know that these should be universal across all platforms/keyboards (can I get confirmation?)

and oh yeah... if I stick the "~" key in there, it causes a bad string escape or something?



Texty version:




Perturbatio(Posted 2009) [#4]
for tilde, use ~~


Brucey(Posted 2009) [#5]
You may have issues on non-english keymappings where normal keys/letters won't return a-z/A-Z.

Hooking up to a EVENT_KEYDOWN and EVENT_KEYCHAR event might be more helpful. With the first you get the real scancode, the second provides the unicode character.


Spacechimp(Posted 2009) [#6]
Thank you Brucey, Perturbatio