similar to b3d getkey()

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

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!

GetChar()?

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?


'atm there is no good reason why this goes up to 255,  so... delete some of it if you like

Global vcharacter$[]=[..
"0" , "1" , "2" , "3" , "4" , "5" , "6" , "7" , "" , "" , ..
"10" , "11" , "12" , "" , "14" , "15" , "16" , "17" , "18" , "19" , ..
"20" , "21" , "22" , "23" , "24" , "25" , "26" , "27" , "28" , "29" , ..
"30" , "31" , " " , "!" , "``" , "#" , "$" , "%" , "&" , "'" , ..
"(" , ")" , "*" , "+" , "," , "-" , "." , "/" , "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" , "[" , "" , "]" , "^" , "_" , "`" , "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" , "{" , "|" , "}" , "126" , "127" , "128" , "129" , ..
"130" , "131" , "132" , "133" , "134" , "135" , "136" , "137" , "138" , "139" , ..
"140" , "141" , "142" , "143" , "144" , "145" , "146" , "147" , "148" , "149" , ..
"150" , "151" , "152" , "153" , "154" , "155" , "156" , "157" , "158" , "159" , ..
"160" , "161" , "162" , "163" , "164" , "165" , "166" , "167" , "168" , "169" , ..
"170" , "171" , "172" , "173" , "174" , "175" , "176" , "177" , "178" , "179" , ..
"180" , "181" , "182" , "183" , "184" , "185" , "186" , "187" , "188" , "189" , ..
"190" , "191" , "192" , "193" , "194" , "195" , "196" , "197" , "198" , "199" , ..
"200" , "201" , "202" , "203" , "204" , "205" , "206" , "207" , "208" , "209" , ..
"210" , "211" , "212" , "213" , "214" , "215" , "216" , "217" , "218" , "219" , ..
"220" , "221" , "222" , "223" , "224" , "225" , "226" , "227" , "228" , "229" , ..
"230" , "231" , "232" , "233" , "234" , "235" , "236" , "237" , "238" , "239" , ..
"240" , "241" , "242" , "243" , "244" , "245" , "246" , "247" , "248" , "249" , ..
"250" , "251" , "252" , "253" , "254" , "255"]


Texty version:

96 `
49 1
50 2
51 3
52 4
53 5
54 6
55 7
56 8
57 9
48 0
45 -
61 =
8 bkspc
9 tab
113 q
119 w
101 e
114 r
116 t
121 y
117 u
105 i
111 o
112 p
91  [
93  ]
92  \
97  a
115 s
100 d
102 f
103 g
104 h
106 j
107 k
108 l
59  ;
39  '
13  enter
122 z
120 x
99  c
118 v
98  b
110 n
109 m
44  ,
46  .
47  /

capitals
81 Q
87 W
69 E
82 R
84 T
89 Y
85 U
73 I
79 O
80 P
65 A
83 S
68 D
70 F
71 G
72 H
74 J
75 K
76 L
90 Z
88 X
67 C
86 V
66 B
78 N
77 M

numpad
47 /
42 *
45 -
43 +
13 enter
55 7
56 8
57 9
52 4
53 5
54 6
49 1
50 2
51 3
48 0
46 .

shift numbers

126 ~   causes bad escape sequence?
33 !
64 @
35 #
36 $
37 %
94 ^
38 &
42 *
40 (
41 )
95 _
43 +
8 bkspc

123 {
125 }
124 |
58  :
34  "
60  <
62  >
63  ?


for tilde, use ~~

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.

Thank you Brucey, Perturbatio