DXtoToken
Revision as of 10:40, 12 March 2008 by imported>Speedo (→The Code)
This code is a resource intended to allow easy text input using Pluggy. It will convert the DirectX scancodes returned by OBSE functions such as GetKeyPress into Pluggy's StringTokens, which can be used with the Pluggy function StringCat to capture input.
Requirements
Technically only the CS is need to use this script in itself, however it is useless without the above two items.
Setup
- Copy the script, and save it in the CS as an object script.
- Create a new activator and assign the script to it. It is not necessary to give the activator a model; it needs only the script and an Editor ID.
- Recommended: DXtoTokenBase
- If you don't already have a "storage cell" to hold activators, create a new cell and add the activator to the cell.
- Edit the properties of the item in the cell:
- Be sure that it is set as a Persistant Reference.
- Give it a unique Reference Editor ID.
- Recommended: DXtoToken
Usage
The script requires that two variables be set:
- dxKey:long - The DirectX keycode that should be converted.
- shift:bool - Whether the shift key(s) are pressed or not.
After activating the activator, the resulting StringToken can be retrieved from the variable sToken:long.
Assuming that you used the recommended naming from the setup section, normal usage will look like the following:
; obtain key pressed and shift's state set DXtoToken.dxKey to keyPressed set DXtoToken.shift to shiftState DXtoToken.activate player 1 set token to DXtoToken.sToken ; use the token
The Code
scn DXtoTokenScript long dxKey long shift long sToken begin onActivate ;dxKey 1 == Escape if (dxKey == 2) if (shift) set sToken to -33 ;! else set sToken to -49 ;1 endif elseif (dxKey == 3) if (shift) set sToken to -64 ;@ else set sToken to -50 ;2 endif elseif (dxKey == 4) if (shift) set sToken to -35 ;# else set sToken to -51 ;3 endif elseif (dxKey == 5) if (shift) set sToken to -36 ;$ else set sToken to -52 ;4 endif elseif (dxKey == 6) if (shift) set sToken to -37 ;% else set sToken to -53 ;5 endif elseif (dxKey == 7) if (shift) set sToken to -94 ;^ else set sToken to -54 ;6 endif elseif (dxKey == 8) if (shift) set sToken to -38 ;& else set sToken to -55 ;7 endif elseif (dxKey == 9) if (shift) set sToken to -42 ;* else set sToken to -56 ;8 endif elseif (dxKey == 10) if (shift) set sToken to -40 ;( else set sToken to -57 ;9 endif elseif (dxKey == 11) if (shift) set sToken to -41 ;) else set sToken to -48 ;0 endif elseif (dxKey == 12) if (shift) set sToken to -95 ;_ else set sToken to -45 ;- endif elseif (dxKey == 13) if (shift) set sToken to -43 ;+ else set sToken to -61 ;= endif ;dxKey 14 == Backspace ;dxKey 15 == Tab elseif (dxKey == 16) set sToken to -113 ;q elseif (dxKey == 17) set sToken to -119 ;w elseif (dxKey == 18) set sToken to -101 ;e elseif (dxKey == 19) set sToken to -114 ;r elseif (dxKey == 20) set sToken to -116 ;t elseif (dxKey == 21) set sToken to -121 ;y elseif (dxKey == 22) set sToken to -117 ;u elseif (dxKey == 23) set sToken to -105 ;i elseif (dxKey == 24) set sToken to -111 ;o elseif (dxKey == 25) set sToken to -112 ;p elseif (dxKey == 26) if (shift) set sToken to -123 ;{ else set sToken to -91 ;[ endif elseif (dxKey == 27) if (shift) set sToken to -125 ;} else set sToken to -93 ;] endif ;dxKey 28 == Enter ;dxKey 29 == LControl elseif (dxKey == 30) set sToken to -97 ;a elseif (dxKey == 31) set sToken to -115 ;s elseif (dxKey == 32) set sToken to -100 ;d elseif (dxKey == 33) set sToken to -102 ;f elseif (dxKey == 34) set sToken to -103 ;g elseif (dxKey == 35) set sToken to -104 ;h elseif (dxKey == 36) set sToken to -106 ;j elseif (dxKey == 37) set sToken to -107 ;k elseif (dxKey == 38) set sToken to -108 ;l elseif (dxKey == 39) if (shift) set sToken to -58 ;: else set sToken to -59 ;; endif elseif (dxKey == 40) if (shift) set sToken to -34 ;" else set sToken to -39 ;' endif ;dxKey 41 == `/~ ;dxKey 42 == LShift elseif (dxKey == 43) if (shift) set sToken to -124 ;| else set sToken to -92 ;\ endif elseif (dxKey == 44) set sToken to -122 ;z elseif (dxKey == 45) set sToken to -120 ;x elseif (dxKey == 46) set sToken to -99 ;c elseif (dxKey == 47) set sToken to -118 ;v elseif (dxKey == 48) set sToken to -98 ;b elseif (dxKey == 49) set sToken to -110 ;n elseif (dxKey == 50) set sToken to -109 ;m elseif (dxKey == 51) if (shift) set sToken to -60 ;< else set sToken to -44 ;, endif elseif (dxKey == 52) if (shift) set sToken to -62 ;> else set sToken to -46 ;. endif elseif (dxKey == 53) if (shift) set sToken to -63 ;? else set sToken to -47 endif elseif (dxKey == 57) set sToken to -32 ;Space else set sToken to -1 ;empty string endif if (((sToken <= -97) && (sToken >= -122)) && shift) set sToken to sToken + 32 ;lowercase char += 32 == uppercase endif set dxKey to 0 set shift to 0 end
Notes
- Essentially all characters on the main keyboard are supported - letters, numbers and symbols.
- Tilde(~) and accent(`) are not supported, since it's not possible to use them anyway due to the console.
- The following keys on the main keyboard are ignored: Esc, Tab, Caps Lock, L/R Shift, L/R Ctrl, L/R Alt, Enter, Backspace, F1-F12.
- All keys off of the main keyboard are ignored: the number pad, arrow keys, Insert, Home, and etc.
- Any time that an unsupported or unrecognized keycode is processed, sToken will be -1, which is an empty string.
- If you want to convert DX keycodes to ASCII values, simply check that sToken is not -1, and the multiply it by -1.