Keyboard Scancode program.

Blitz3D Forums/Blitz3D Beginners Area/Keyboard Scancode program.

collimic(Posted 2012) [#1]
I am looking for a way to open a program window,
The press a key on the keyboard and have it tell me both what key I pressed as well as the correct scan code.

Is there something build in to do I have to create a 300 line program that says :

If keyhit( 1 ) then print "ESC Scan Code 1"


and so on.

Thank you in advance for your help
The Blitz Beginner.

Last edited 2012

Last edited 2012

Last edited 2012

Last edited 2012


Yasha(Posted 2012) [#2]
Use GetKey:

Local k = GetKey() : Print "Scan code " + k


Unlike WaitKey, GetKey does not pause the program. You can safely use it as part of an input system for an interactive scene.

If you also want the key names, then yes you need a 300-odd line include with an array declaration that has their names, because there's no forumlaic mapping from scancodes to name strings. Several such files already exist in the code archives, though.

Last edited 2012


Kryzon(Posted 2012) [#3]
If you want to test for all keys (chars, numbers), including SHIFT, ALT, CTRL etc. use the following:

[bbcode]
Local FPSTimer = CreateTimer(60)
While Not KeyHit(1) ;Press ESC to quit.
WaitTimer(FPSTimer)

For scanCode = 2 To 255 ;We skip the '1' because it's being used to quit the app.
If KeyDown(scanCode) Then
Cls
Text 0,0,scanCode
EndIf
Next
Wend[/bbcode]

Last edited 2012


collimic(Posted 2012) [#4]
This last one seams to work great.
If I could just figure out how to add an input$ so when I pressed it said
text,0,0,scanCode + input$

please note that is is pseudo code and not real code.
If I knew the real code I would not be asking for the help.
The waitkey or getkey would have worked if i needed the ascii value but I want the Name and the scancode.

Thank you all again for your help.


Kryzon(Posted 2012) [#5]
If you reaaally need the key's "name" - that is, a string that "represents" the key such as "Left CTRL" - you need an array that has each key name set for each scancode index, so it works like a simple function: you input the scancode and it gives you the key's name.

You can use this for array (original code by Perturbatio):

Usage goes like this: Keynames(scanCode)

Also, check out the User-Input Code Archive entries.

Last edited 2012


collimic(Posted 2012) [#6]
Took a little play around but I got exactly what I needed.
Thank you so much.
Local FPSTimer = CreateTimer(60)
Include "../include/keyname.bb"

While Not KeyHit(1) ;Press ESC to quit.
        WaitTimer(FPSTimer)
   
     For scanCode = 2 To 255 ;We skip the '1' because it's being used to quit the app.
                If KeyDown(scanCode) Then
                        Cls
			Text 0,0,"ScanCode = " + scanCode
			Text 0,15,"KeyName = " + Keynames(scanCode)
						
                EndIf
        Next
	 
Wend

The included keyname.bb is the array you pointed out for me.
Once again thank you so much.
Sorry for being such a beginner but I am going to make this work out in the long run.