Thanks Scraggle, Bobbel and Mobiius!
These are all fine solutions, however they don't protect you against your code running on a machine whose keyboard doesn't match your array or constants.
load dll "user32.dll", 1
filename$ = "scancodeMap.txt"
set dir get dir$()
if file exist(filename$) then delete file filename$
open to write 1, filename$
for n=0 to 255
line$ = GetKeyName(n) + "=" + str$(n)
write string 1, line$
next
close file 1
end
function GetKeyName(sCode as integer)
name$ = space$(64)
if not call dll(1, "GetKeyNameTextA", sCode << 16, name$, 64) then name$ = "<#" + str$(sCode) + "#>"
endfunction name$
My situation is this: We conviced our boss at work to have a LAN party for a departmental social. His condition was that the software has to be free. So far we've got Quake 3 and Warzone 2100 and a special surprise game. The surprise game is a networked vehicular shooter game I wrote a couple years ago. It works fine on my PC but we have very different keyboards at work. They're not the standard 104/5 key I have at home. This is why I need to be smart with how I convert ascii to scancodes.
I need the program to read a line in my ini file like "FORWARD=W" and then be able to ask the PC it's running on what scancode matches "W", for example. On my PC at home "W" is scancode 17, but there's no guarantee that will be true on the fancy keyboards where I work.
As a last resort I'll run the code in the snippet above and generate a machine-specific keymap and then load the ini file like usual. However - that seems like a step too far. My instinct tells me that, if I can ask User32.dll to convert a scancode to a string value, then I should be able to do it the other way around - give it a string and get a scancode back.
Ending a sentence with a French word is so passé