Rebinding keys in-game (thanks Chosker!)

I’m not much of a UI designer, but I can get the job done.

I’ve had a lot of players asking if they can rebind keys. Some people use AZERTY keyboards. Some find the camera movement counterintuitive. And some, for whatever reason, just want to use different keys. And until now, I’ve had to tell people to edit the game’s input.ini file. Well, as of yesterday, they can rebind the keys in-game.

I just wanted to say thanks to Chosker for getting the process started with this old post: How to re-bind keys in-game - Epic Games Forums. It’s still getting used today. And in case anyone else ever wants to make a key-rebinding menu, let me contribute this helper function:

UnrealScript



function name GetKeyNameFromCode(int KeyCode)
{
    switch(KeyCode)
    {
    case 8:
        return 'Backspace';
    case 9:
        return 'Tab';
    case 13:
        return 'Enter';
    case 16:
        return 'Shift';
    case 17:
        return 'Control';
    case 20:
        return 'CapsLock';
    case 27:
        return 'Escape';
    case 32:
        return 'Spacebar';
    case 33:
        return 'PageUp';
    case 34:
        return 'PageDown';
    case 35:
        return 'End';
    case 36:
        return 'Home';
    case 37:
        return 'LeftArrow';
    case 38:
        return 'UpArrow';
    case 39:
        return 'RightArrow';
    case 40:
        return 'DownArrow';
    case 45:
        return 'Insert';
    case 46:
        return 'Delete';
    case 144:
        return 'NumLock';
    case 145:
        return 'ScrollLock';
    case 19:
        return 'Pause';
    case 65:
        return 'A';
    case 66:
        return 'B';
    case 67:
        return 'C';
    case 68:
        return 'D';
    case 69:
        return 'E';
    case 70:
        return 'F';
    case 71:
        return 'G';
    case 72:
        return 'H';
    case 73:
        return 'I';
    case 74:
        return 'J';
    case 75:
        return 'K';
    case 76:
        return 'L';
    case 77:
        return 'M';
    case 78:
        return 'N';
    case 79:
        return 'O';
    case 80:
        return 'P';
    case 81:
        return 'Q';
    case 82:
        return 'R';
    case 83:
        return 'S';
    case 84:
        return 'T';
    case 85:
        return 'U';
    case 86:
        return 'V';
    case 87:
        return 'W';
    case 88:
        return 'X';
    case 89:
        return 'Y';
    case 90:
        return 'Z';
    case 48:
        return 'zero';
    case 49:
        return 'one';
    case 50:
        return 'two';
    case 51:
        return 'three';
    case 52:
        return 'four';
    case 53:
        return 'five';
    case 54:
        return 'six';
    case 55:
        return 'seven';
    case 56:
        return 'eight';
    case 57:
        return 'nine';
    case 186:
        return 'Semicolon';
    case 187:
        return 'Equals';
    case 189:
        return 'Underscore';
    case 191:
        return 'Slash';
    case 192:
        return 'Tilde';
    case 219:
        return 'LeftBracket';
    case 220:
        return 'Backslash';
    case 221:
        return 'RightBracket';
    case 222:
        return 'Quote';
    case 188:
        return 'Comma';
    case 190:
        return 'Period';
    case 96:
        return 'NumPadZero';
    case 97:
        return 'NumPadOne';
    case 98:
        return 'NumPadTwo';
    case 99:
        return 'NumPadThree';
    case 100:
        return 'NumPadFour';
    case 101:
        return 'NumPadFive';
    case 102:
        return 'NumPadSix';
    case 103:
        return 'NumPadSeven';
    case 104:
        return 'NumPadEight';
    case 105:
        return 'NumPadNine';
    case 106:
        return 'Multiply';
    case 107:
        return 'Add';
    case 13:
        return 'Enter';
    case 109:
        return 'Subtract';
    case 110:
        return 'Decimal';
    case 111:
        return 'Divide';
    default:
        return '';
    }
}


This converts ActionScript 3 key codes into Unreal key names. It was kind of a pain for me to look up both sets and create a dictionary by hand. I left out the Function keys though. My own game reserves those and won’t let you overwrite them. But it shouldn’t be too hard to add a few more entries to that switch statement.

Basically, the way it works is in the Flash movie, when the player clicks on Rebind Keys…

ActionScript



stage.addEventListener(KeyboardEvent.KEY_DOWN, reportKeyDown);

function reportKeyDown(event:KeyboardEvent):void 
{
    // send event.keyCode to UnrealScript with an ExternalInterface.call
}


…an event listener calls the function reportKeyDown when the player presses a key. That function tells UnrealScript which key code was detected, and the UnrealScript function at top translates that into a key name, which I then send to Chosker’s function.

I did have to do a little more work: clearing other keys that had the same function, updating the button labels with the UnrealScript key names, and maybe a few other steps. But what you see is basically the heart of it.

Everything seems to be working well so far. Thanks for your help Chosker!

you are welcome :slight_smile:
glad my old stuff is still helpful even now