scan-codes to ASCII conversion

Blitz3D Forums/Blitz3D Beginners Area/scan-codes to ASCII conversion

MusicianKool(Posted 2011) [#1]
So why isn't there a function that converts scan-codes to ASCII codes or characters. I'm going to have to make it if there isn't a simple way to do this already.


Yasha(Posted 2011) [#2]
1) Because there isn't one simple formula that will do this; it has to involve lookups due to completely unrelated layouts.

2) They don't match up perfectly: half of the letters of the alphabet don't even have matching scancodes, because they need to be entered with Shift, while Shift itself doesn't have an ASCII code, as with the other control keys.

3) Most of the use cases for this are covered by GetKey() (which also handles Shift correctly).

There are a couple of scancode-name arrays in the Code archives, if you really want something like this, but I don't think there is a general solution, for the given reason: that ASCII and scancodes don't really match up.


MusicianKool(Posted 2011) [#3]
Yea, that is what I thought. That's why I already started the converter, ie. one really big select true .


H&K(Posted 2011) [#4]
1) Array[scan number] = Ascii

2) Why?


MusicianKool(Posted 2011) [#5]
cause getkey() doesn't repeat an input. example
if you hold down 'e', getkey() does:
e
but i want
eeeeeeeeeeeeeeeeeeee
to happen as long as it is held down.

and now i'm pissed. i had it done and protean crashed and its gone. should have saved! aww funny.


MusicianKool(Posted 2011) [#6]
Well maybe that was a good thing, here is the code to do Scan Code to ASCII.



Last edited 2011


H&K(Posted 2011) [#7]
http://www.blitzbasic.com/b3ddocs/command.php?name=KeyDown&ref=goto
(KeyDown (scancode))


MusicianKool(Posted 2011) [#8]
sorry h&k. that is a given, but doesnt work for my needs..


Floyd(Posted 2011) [#9]
This is like the difference between KeyDown ( is a key currently held down ) and KeyHit ( has a key been pressed and released ).

Making it work for a variety of keyboards, such as German and UK and many others, would be a huge job.

It also doesn't account for Caps Lock.


Yasha(Posted 2011) [#10]
I can't help but think that there must be some much easier way to do this using Windows API commands (if only I knew the Windows API by rote...). Instead of using KeyDown, which is pretty inefficient, you ought to be able to query this sort of thing directly from the OS and essentially replicate GetKey, right?


Midimaster(Posted 2011) [#11]
you could combine the converter above with this "repeat-recognition":

Graphics 800,600

Type TGetKey
	Field PressTime% , Pressfirst%
End Type

While Indx <> 999
	Read Indx
	If indx <> 999 Then
		Read ascii
		Read scn 
		ScanCode_Array(scn,Indx)=ascii
	EndIf
Wend

Repeat
	a=GetKeyII()
	If a Then 
		Print MilliSecs() +" " + a ;+ " " + ScanToAscII(a) 
	EndIf
Until KeyHit(key_Escape)


Function GetKeyII()
	Local Key.TGetKey=First TGetKey
	If Key=Null Key.TGetKey=New TGetKey

	If Key\PressTime<MilliSecs()
		Key\PressTime=MilliSecs()+500-Key\PressFirst
		If Key\PressFirst=0 Key\PressFirst=400
		For i=1 To 255
			If KeyDown(i)
				Return i
			EndIf
		Next
		Key\PressFirst=0
		Key\PressTime=MilliSecs()+30
	EndIf
End Function



Last edited 2011


Yasha(Posted 2011) [#12]
Here we go, this is what I was thinking had to exist somewhere: Windows API ToAscii

You'll need to read up on virtual key codes - not the same as scancodes! - to make use of it. (See Keyboard Input index.)

There's also a ToAsciiEx, which can take a keyboard locale parameter, and a ToUnicode.

...then you can do something like this example.

Last edited 2011


MusicianKool(Posted 2011) [#13]
I figured I should post the code i used to make the data list. very simple, just press: esc,`,1,2,3,4.... all the keys and there shift used counterpart and done. quit the program by pressing F1 (cause F1 doesn't have an ascii code).
it outputs it to the specified file, then open that file copy and replace the existing data list in the above converter.




MusicianKool(Posted 2011) [#14]
Well the above, sadly, isn't working correctly. So I am trying Yasha's win api suggestion. Yet there is a problem, I cannot get the key\s that are being pressed. I've tried various data types for result and most lead to an error. bank's work but i cannot read it once it is returned. It seems to destroy the bank, i cannot get the size of the bank or access it in any way. Well it seems that it changes what the result bank is, it makes it a 16bit WORD data type, I'm not sure how to get this fix. hmmm

you will need the user32.dll decls found in the code archive I think.


Last edited 2011

Last edited 2011

Last edited 2011