Input Script Improvment

Post » Sat Oct 31, 2015 12:03 pm

Does anyone want to help me make this function better. I would like to get the friendly string name of a DXScanCode. Below is the first thing I thought of. The higher aiKeyCode number gets the longer it takes to process though.

ScriptName GlobalScript Hiddenstring Function GetKeyName(int aiKeyCode) Global    If(aiKeyCode == 1)        return "Escape"    ElseIf(aiKeyCode == 2)        return "1"    ElseIf(aiKeyCode == 3)        return "2"    ElseIf(aiKeyCode == 4)        return "3"    ElseIf(aiKeyCode == 5)        return "4"    ElseIf(aiKeyCode == 6)        return "5"    ElseIf(aiKeyCode == 7)        return "6"    ElseIf(aiKeyCode == 8)        return "7"    ElseIf(aiKeyCode == 9)        return "8"    ElseIf(aiKeyCode == 10)        return "9"    ElseIf(aiKeyCode == 11)        return "0"    ElseIf(aiKeyCode == 12)        return "Minus"    ElseIf(aiKeyCode == 13)        return "Equals"    ElseIf(aiKeyCode == 14)        return "Backspace"    ElseIf(aiKeyCode == 15)        return "Tab"    ElseIf(aiKeyCode == 16)        return "Q"    ElseIf(aiKeyCode == 17)        return "W"    ElseIf(aiKeyCode == 18)        return "E"    ElseIf(aiKeyCode == 19)        return "R"    ElseIf(aiKeyCode == 20)        return "T"    ElseIf(aiKeyCode == 21)        return "Y"    ElseIf(aiKeyCode == 22)        return "U"    ElseIf(aiKeyCode == 23)        return "I"    ElseIf(aiKeyCode == 24)        return "O"    ElseIf(aiKeyCode == 25)        return "P"    ElseIf(aiKeyCode == 26)        return "Left Bracket"    ElseIf(aiKeyCode == 27)        return "Right Bracket"    ElseIf(aiKeyCode == 28)        return "Enter"    ElseIf(aiKeyCode == 29)        return "Left Control"    ElseIf(aiKeyCode == 30)        return "A"    ElseIf(aiKeyCode == 31)        return "S"    ElseIf(aiKeyCode == 32)        return "D"    ElseIf(aiKeyCode == 33)        return "F"    ElseIf(aiKeyCode == 34)        return "G"    ElseIf(aiKeyCode == 35)        return "H"    ElseIf(aiKeyCode == 36)        return "J"    ElseIf(aiKeyCode == 37)        return "K"    ElseIf(aiKeyCode == 38)        return "L"    ElseIf(aiKeyCode == 39)        return "Semicolon"    ElseIf(aiKeyCode == 40)        return "Apostrophe"    ElseIf(aiKeyCode == 41)        return "~ (Console)"    ElseIf(aiKeyCode == 42)        return "Left Shift"    ElseIf(aiKeyCode == 43)        return "Back Slash"    ElseIf(aiKeyCode == 44)        return "Z"    ElseIf(aiKeyCode == 45)        return "X"    ElseIf(aiKeyCode == 46)        return "C"    ElseIf(aiKeyCode == 47)        return "V"    ElseIf(aiKeyCode == 48)        return "B"    ElseIf(aiKeyCode == 49)        return "N"    ElseIf(aiKeyCode == 50)        return "M"    ElseIf(aiKeyCode == 51)        return "Comma"    ElseIf(aiKeyCode == 52)        return "Period"    ElseIf(aiKeyCode == 53)        return "Forward Slash "    ElseIf(aiKeyCode == 54)        return "Right Shift"    ElseIf(aiKeyCode == 55)        return "NUM*"    ElseIf(aiKeyCode == 56)        return "Left Alt"    ElseIf(aiKeyCode == 57)        return "Spacebar"    ElseIf(aiKeyCode == 58)        return "Caps Lock"    ElseIf(aiKeyCode == 59)        return "F1"    ElseIf(aiKeyCode == 60)        return "F2"    ElseIf(aiKeyCode == 61)        return "F3"    ElseIf(aiKeyCode == 62)        return "F4"    ElseIf(aiKeyCode == 63)        return "F5"    ElseIf(aiKeyCode == 64)        return "F6"    ElseIf(aiKeyCode == 65)        return "F7"    ElseIf(aiKeyCode == 66)        return "F8"    ElseIf(aiKeyCode == 67)        return "F9"    ElseIf(aiKeyCode == 68)        return "F10"    ElseIf(aiKeyCode == 69)        return "Num Lock"    ElseIf(aiKeyCode == 70)        return "Scroll Lock"    ElseIf(aiKeyCode == 71)        return "NUM7"    ElseIf(aiKeyCode == 72)        return "NUM8"    ElseIf(aiKeyCode == 73)        return "NUM9"    ElseIf(aiKeyCode == 74)        return "NUM Minus"    ElseIf(aiKeyCode == 75)        return "NUM4"    ElseIf(aiKeyCode == 76)        return "NUM5"    ElseIf(aiKeyCode == 77)        return "NUM6"    ElseIf(aiKeyCode == 78)        return "NUM Plus"    ElseIf(aiKeyCode == 79)        return "NUM1"    ElseIf(aiKeyCode == 80)        return "NUM2"    ElseIf(aiKeyCode == 81)        return "NUM3"    ElseIf(aiKeyCode == 82)        return "NUM0"    ElseIf(aiKeyCode == 83)        return "NUM Period"    ; skipped    ElseIf(aiKeyCode == 87)        return "F11"    ElseIf(aiKeyCode == 88)        return "F12"    ; skipped    ElseIf(aiKeyCode == 156)        return "NUM Enter"    ElseIf(aiKeyCode == 157)        return "Right Control"    ; skipped    ElseIf(aiKeyCode == 181)        return "NUM Forward Slash"    ; skipped    ElseIf(aiKeyCode == 184)        return "Right Alt"    ; skipped    ElseIf(aiKeyCode == 197)        return "Pause"    ; skipped    ElseIf(aiKeyCode == 199)        return "Home"    ElseIf(aiKeyCode == 200)        return "Up Arrow"    ElseIf(aiKeyCode == 201)        return "Page Up"    ; skipped    ElseIf(aiKeyCode == 203)        return "Left Arrow"    ElseIf(aiKeyCode == 205)        return "Right Arrow"    ; skipped    ElseIf(aiKeyCode == 207)        return "End"    ElseIf(aiKeyCode == 208)        return "Down Arrow"    ElseIf(aiKeyCode == 209)        return "Page Down"    ElseIf(aiKeyCode == 210)        return "Insert"    ElseIf(aiKeyCode == 211)        return "Delete"    ; skipped    ElseIf(aiKeyCode == 256)        return "Left Mouse Button"    ElseIf(aiKeyCode == 257)        return "Right Mouse Button"    ElseIf(aiKeyCode == 258)        return "Middle/Wheel Mouse Button"    ElseIf(aiKeyCode == 259)        return "Mouse Button 3"    ElseIf(aiKeyCode == 260)        return "Mouse Button 4"    ElseIf(aiKeyCode == 261)        return "Mouse Button 5"    ElseIf(aiKeyCode == 262)        return "Mouse Button 6"    ElseIf(aiKeyCode == 263)        return "Mouse Button 7"    ElseIf(aiKeyCode == 264)        return "Mouse Wheel Up"    ElseIf(aiKeyCode == 265)        return "Mouse Wheel Down"    ElseIf(aiKeyCode == 266)        return "Pad Up"    ElseIf(aiKeyCode == 267)        return "Pad Down"    ElseIf(aiKeyCode == 268)        return "Pad Left"    ElseIf(aiKeyCode == 269)        return "Pad Right"    ElseIf(aiKeyCode == 270)        return "Start"    ElseIf(aiKeyCode == 271)        return "Back"    ElseIf(aiKeyCode == 272)        return "Left Thumb"    ElseIf(aiKeyCode == 273)        return "Right Thumb"    ElseIf(aiKeyCode == 274)        return "Left Shoulder"    ElseIf(aiKeyCode == 275)        return "Right Shoulder"    ElseIf(aiKeyCode == 276)        return "A"    ElseIf(aiKeyCode == 277)        return "B"    ElseIf(aiKeyCode == 279)        return "X"    ElseIf(aiKeyCode == 279)        return "Y"    ElseIf(aiKeyCode == 280)        return "Left Trigger"    ElseIf(aiKeyCode == 281)        return "Right Trigger"    EndIf    return ""EndFunction
User avatar
Ilona Neumann
 
Posts: 3308
Joined: Sat Aug 19, 2006 3:30 am

Post » Sat Oct 31, 2015 11:43 am

Try to create multiple stacks based on the value of aiKeyCode.

Example:

if (aiKeyCode>=200)    ;all the conditions for aiKeyCode>=200 hereendifif (aiKeyCode>=100 && aiKeyCode <200)    ;all the conditions for aiKeyCode from 100 to 199 hereendifif (aiKeyCode>0 && aiKeyCode <100)    ;all the conditions for aiKeyCode from 1 to 99 hereendif

Use more stacks if necessary.

User avatar
latrina
 
Posts: 3440
Joined: Mon Aug 20, 2007 4:31 pm

Post » Sat Oct 31, 2015 2:29 pm

You can always code it in a binary search style for optimal performance.

if code < 64  if code < 32    if code < 16      if code < 8        if code < 4          if code < 2            if code == 0              return "NULL"            else ; 1              return "Escape"            endif          else            if code == 2              return "1"            else ; 3              return "2"            endif          endif        else...

It's not pretty but you'll get a constant response time.

User avatar
Sxc-Mary
 
Posts: 3536
Joined: Wed Aug 23, 2006 12:53 pm

Post » Sat Oct 31, 2015 1:18 pm

If you don't need to make it global, then using three string arrays (max size is 128) as properties would probably be the simplest solution.

User avatar
Veronica Flores
 
Posts: 3308
Joined: Mon Sep 11, 2006 5:26 pm

Post » Sun Nov 01, 2015 2:14 am

Thanks for the answers everyone. I went with splitting it into groups because I dont fully understand how to do a binary search. Maybe you could explain this some more cdcooley

User avatar
jeremey wisor
 
Posts: 3458
Joined: Mon Oct 22, 2007 5:30 pm

Post » Sat Oct 31, 2015 2:41 pm

OK, since you asked.

Binary search is just the strategy of always asking a question that cuts the list of possible choices in half. Since there are about 120 usable key codes (and using powers of 2 makes things easier), I start with the test "if code < 64" and if the answer to that is yes, then the next question would be "is it less than 32". While it's the most efficient way to hard code the results, it's pretty ugly and almost completely unreadable. In theory you just keep doing what I did for those first few tests, in the "else" sections that follow.

For my own mod I simply divided the list into groups just like you did. :smile:

But just for fun, here's a hybrid approach that's slightly more readable than the full binary algorithm. I broke the first half of the list down into groups of 8, but got tired and only did the second half in groups of 16. Maybe that will help you see the pattern. :)

If(aiKeyCode < 64)  If(aiKeyCode < 32)    If(aiKeyCode < 16)      If(aiKeyCode < 8)        If(aiKeyCode == 1)          return "Escape"        ElseIf(aiKeyCode == 2)          return "1"        ElseIf(aiKeyCode == 3)          return "2"        ElseIf(aiKeyCode == 4)          return "3"        ElseIf(aiKeyCode == 5)          return "4"        ElseIf(aiKeyCode == 6)          return "5"        ElseIf(aiKeyCode == 7)          return "6"        EndIf      Else ; 8-15        If(aiKeyCode == 8)          return "7"        ElseIf(aiKeyCode == 9)          return "8"        ElseIf(aiKeyCode == 10)          return "9"        ElseIf(aiKeyCode == 11)          return "0"        ElseIf(aiKeyCode == 12)          return "Minus"        ElseIf(aiKeyCode == 13)          return "Equals"        ElseIf(aiKeyCode == 14)          return "Backspace"        ElseIf(aiKeyCode == 15)          return "Tab"        EndIf      EndIf    Else  ; 16-31      If(aiKeyCode < 24)        If(aiKeyCode == 16)          return "Q"        ElseIf(aiKeyCode == 17)          return "W"        ElseIf(aiKeyCode == 18)          return "E"        ElseIf(aiKeyCode == 19)          return "R"        ElseIf(aiKeyCode == 20)          return "T"        ElseIf(aiKeyCode == 21)          return "Y"        ElseIf(aiKeyCode == 22)          return "U"        ElseIf(aiKeyCode == 23)          return "I"        EndIf      Else ; 24-31        If(aiKeyCode == 24)          return "O"        ElseIf(aiKeyCode == 25)          return "P"        ElseIf(aiKeyCode == 26)          return "Left Bracket"        ElseIf(aiKeyCode == 27)          return "Right Bracket"        ElseIf(aiKeyCode == 28)          return "Enter"        ElseIf(aiKeyCode == 29)          return "Left Control"        ElseIf(aiKeyCode == 30)          return "A"        Else ;If(aiKeyCode == 31)          return "S"        EndIf      EndIf    EndIf  Else  ; 32 - 63    If(aiKeyCode < 48)      If(aiKeyCode < 40)        If(aiKeyCode == 32)          return "D"        ElseIf(aiKeyCode == 33)          return "F"        ElseIf(aiKeyCode == 34)          return "G"        ElseIf(aiKeyCode == 35)          return "H"        ElseIf(aiKeyCode == 36)          return "J"        ElseIf(aiKeyCode == 37)          return "K"        ElseIf(aiKeyCode == 38)          return "L"        ElseIf(aiKeyCode == 39)          return "Semicolon"        EndIf      Else ; 40-47        If(aiKeyCode == 40)          return "Apostrophe"        ElseIf(aiKeyCode == 41)          return "~ (Console)"        ElseIf(aiKeyCode == 42)          return "Left Shift"        ElseIf(aiKeyCode == 43)          return "Back Slash"        ElseIf(aiKeyCode == 44)          return "Z"        ElseIf(aiKeyCode == 45)          return "X"        ElseIf(aiKeyCode == 46)          return "C"        Else ;If(aiKeyCode == 47)          return "V"        EndIf      EndIf    Else  ; 48-63      If(aiKeyCode < 56)        If(aiKeyCode == 48)          return "B"        ElseIf(aiKeyCode == 49)          return "N"        ElseIf(aiKeyCode == 50)          return "M"        ElseIf(aiKeyCode == 51)          return "Comma"        ElseIf(aiKeyCode == 52)          return "Period"        ElseIf(aiKeyCode == 53)          return "Forward Slash "        ElseIf(aiKeyCode == 54)          return "Right Shift"        Else ;If(aiKeyCode == 55)          return "NUM*"        EndIf      Else  ; 56-63        If(aiKeyCode == 56)          return "Left Alt"        ElseIf(aiKeyCode == 57)          return "Spacebar"        ElseIf(aiKeyCode == 58)          return "Caps Lock"        ElseIf(aiKeyCode == 59)          return "F1"        ElseIf(aiKeyCode == 60)          return "F2"        ElseIf(aiKeyCode == 61)          return "F3"        ElseIf(aiKeyCode == 62)          return "F4"        Else ;If(aiKeyCode == 63)          return "F5"        EndIf      EndIf    EndIf  EndifElse ; the rest starting at 64 (but the number sequence has gaps now)  If(aiKeyCode < 207) ; a group of 32    If(aiKeyCode < 207) ; 64-79 (a group of 16)      If(aiKeyCode == 64)        return "F6"      ElseIf(aiKeyCode == 65)        return "F7"      ElseIf(aiKeyCode == 66)        return "F8"      ElseIf(aiKeyCode == 67)        return "F9"      ElseIf(aiKeyCode == 68)        return "F10"      ElseIf(aiKeyCode == 69)        return "Num Lock"      ElseIf(aiKeyCode == 70)        return "Scroll Lock"      ElseIf(aiKeyCode == 71)        return "NUM7"      ElseIf(aiKeyCode == 72)        return "NUM8"      ElseIf(aiKeyCode == 73)        return "NUM9"      ElseIf(aiKeyCode == 74)        return "NUM Minus"      ElseIf(aiKeyCode == 75)        return "NUM4"      ElseIf(aiKeyCode == 76)        return "NUM5"      ElseIf(aiKeyCode == 77)        return "NUM6"      ElseIf(aiKeyCode == 78)        return "NUM Plus"      Else ;If(aiKeyCode == 79)        return "NUM1"      EndIf    Else ; the next 16 starting at 80      If(aiKeyCode == 80)        return "NUM2"      ElseIf(aiKeyCode == 81)        return "NUM3"      ElseIf(aiKeyCode == 82)        return "NUM0"      ElseIf(aiKeyCode == 83)        return "NUM Period"      ; skipped      ElseIf(aiKeyCode == 87)        return "F11"      ElseIf(aiKeyCode == 88)        return "F12"      ; skipped      ElseIf(aiKeyCode == 156)        return "NUM Enter"      ElseIf(aiKeyCode == 157)        return "Right Control"      ; skipped      ElseIf(aiKeyCode == 181)        return "NUM Forward Slash"      ; skipped      ElseIf(aiKeyCode == 184)        return "Right Alt"      ; skipped      ElseIf(aiKeyCode == 197)        return "Pause"      ; skipped      ElseIf(aiKeyCode == 199)        return "Home"      ElseIf(aiKeyCode == 200)        return "Up Arrow"      ElseIf(aiKeyCode == 201)        return "Page Up"      ; skipped      ElseIf(aiKeyCode == 203)        return "Left Arrow"      ElseIf(aiKeyCode == 205)        return "Right Arrow"      EndIf      ; 206 skipped    EndIf  Else ; Another group of 32 broken into two parts    If(aiKeyCode <       If(aiKeyCode == 207)        return "End"      ElseIf(aiKeyCode == 208)        return "Down Arrow"      ElseIf(aiKeyCode == 209)        return "Page Down"      ElseIf(aiKeyCode == 210)        return "Insert"      ElseIf(aiKeyCode == 211)        return "Delete"      ; skipped      ElseIf(aiKeyCode == 256)        return "Left Mouse Button"      ElseIf(aiKeyCode == 257)        return "Right Mouse Button"      ElseIf(aiKeyCode == 258)        return "Middle/Wheel Mouse Button"      ElseIf(aiKeyCode == 259)        return "Mouse Button 3"      ElseIf(aiKeyCode == 260)        return "Mouse Button 4"      ElseIf(aiKeyCode == 261)        return "Mouse Button 5"      ElseIf(aiKeyCode == 262)        return "Mouse Button 6"      ElseIf(aiKeyCode == 263)        return "Mouse Button 7"      ElseIf(aiKeyCode == 264)        return "Mouse Wheel Up"      ElseIf(aiKeyCode == 265)        return "Mouse Wheel Down"      ElseIf(aiKeyCode == 266)        return "Pad Up"      EndIf    Else ; the last few      If(aiKeyCode == 267)        return "Pad Down"      ElseIf(aiKeyCode == 268)        return "Pad Left"      ElseIf(aiKeyCode == 269)        return "Pad Right"      ElseIf(aiKeyCode == 270)        return "Start"      ElseIf(aiKeyCode == 271)        return "Back"      ElseIf(aiKeyCode == 272)        return "Left Thumb"      ElseIf(aiKeyCode == 273)        return "Right Thumb"      ElseIf(aiKeyCode == 274)        return "Left Shoulder"      ElseIf(aiKeyCode == 275)        return "Right Shoulder"      ElseIf(aiKeyCode == 276)        return "A"      ElseIf(aiKeyCode == 277)        return "B"      ElseIf(aiKeyCode == 279)        return "X"      ElseIf(aiKeyCode == 279)        return "Y"      ElseIf(aiKeyCode == 280)        return "Left Trigger"      ElseIf(aiKeyCode == 281)        return "Right Trigger"      EndIf    EndIf  EndIfEndIfreturn "???" ; catch anything else as unknown

(And I didn't actually try to run that code, so I might have made a mistake.)

User avatar
Reven Lord
 
Posts: 3452
Joined: Mon May 21, 2007 9:56 pm


Return to V - Skyrim