scan-codes to ASCII conversion
Blitz3D Forums/Blitz3D Beginners Area/scan-codes to ASCII conversion
| ||
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. |
| ||
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. |
| ||
Yea, that is what I thought. That's why I already started the converter, ie. one really big select true . |
| ||
1) Array[scan number] = Ascii 2) Why? |
| ||
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. |
| ||
Well maybe that was a good thing, here is the code to do Scan Code to ASCII. Last edited 2011 |
| ||
http://www.blitzbasic.com/b3ddocs/command.php?name=KeyDown&ref=goto (KeyDown (scancode)) |
| ||
sorry h&k. that is a given, but doesnt work for my needs.. |
| ||
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. |
| ||
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? |
| ||
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 |
| ||
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 |
| ||
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. |
| ||
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 |