Game Development Community

Custom Key binds?

by Jacob Vann · in Torque Game Builder · 08/14/2006 (8:05 pm) · 0 replies

Hey Guys -

I'm working on the "Options" gui for my game. One of the things I'd like for them to be able to set is the keyboard layout for the game, essentially creating custom key binds.

There are only 3 "events" that the player needs to worry about: "left", "right," and "shoot."

There are a few ways I can go about this:

1. Create a drop-down menu of every possible keyboard event that I want to bind to, like "space", "lcontrol", "z", etc. This has obvious problems since I don't really want to have the player scroll through all 101 choices.

2. Create a text box where the player essentially "types" what they want. This works great for "w", "d" and "s", but for "lcontrol" the player might not know what to put there, and I would have to handle any invalid choices the player typed in.

3. The ideal way I can think of is have 3 buttons, each for "Move Left", "Move Right", and "Shoot." When the player clicks on any of them it'll bring up a popup window saying "press the key for " Then my script will capture the keyboard event as an event name and store it for making a key bind later.

However, for the life of me I can't think of an elegant way to do it. I can bind an action called CaptureInput to the "anykey" event, but this doesn't tell me anything about *what* key was pressed, just that *any* key was pressed. Is there a way to set it up such that the event name that triggered the function call is passed instead of just "1" or "0"?

A kludgy way to do it is to make a bunch of keybinds like the following:

myMap.bindCmd("keyboard", "a", "keyOn_a();", "keyOff_a();");
...
myMap.bindCmd("keyboard", "left", "keyOn_left();", "keyOff_left();");

And have those keyOn and keyOff functions set a $keyPressed variable or something similar.

I know a lot of you have run into this very problem. Any advice on how best to set up custom key binds?

This is a throwaway project - I've made it simple so that I will actually be able to finish it - so I'm not against hacking a solution to "make it work." I'm just hoping I can do this elegantly. One thing I'd love to try and build eventually (not for this project) would be a custom gui that displays a standard keyboard layout, and gives draggable icons representing the actions of the game. You just drag the action to the key that you want to trigger it, and when you can save your custom layout to a profile. The old Mac OS inputSprockets had something very similar to this.

... Or I can simply stick to "mouse only" games and avoid this problem in the future. :)

EDIT: Looks like the Level Editor's option panel does this. Now my question will have to be "how did they do it?"