Key Bind Item Examples

Key Bind items are used to bind a keystroke to a cvar used by the program to perform an action in the game. These items are text based and display a description of the keystroke to the right of any defined text. As is usual in any text based item you have to define all the text commands or the item won't display properly.

    itemDef 
    {
name look
type ITEM_TYPE_BIND
text "Lookup:"
cvar "+lookup"
rect 99 50 256 20
textalign ITEM_ALIGN_RIGHT
textalignx 128
textaligny 17
textscale .333
forecolor 1 1 1 1
visible MENU_TRUE
}

The cvar command assigns the "+lookup" cvar to the Key Bind item. This cvar controls the key used to make the player look up in the game. The description of the key bound to this cvar is displayed 8 pixels to the right of the text defined by the text command. When you click on this item it shifts into assignment mode and waits for you to press the key you wish to bind to the cvar. Pressing the key assigns it to the cvar and the item shifts back to nonassignment mode.

In the Key Bind item itself you can not tell if you are in assignment mode or not. Instead an internal flag is used that can be accessed by a UI_KEYBINDSTATUS Ownerdraw item. This item will display a text string telling you the assignment mode status of any Key Bind item defined in the menu that currently has the focus.

    itemDef 
    {
name keyBindStatus
rect 0 15 640 30
ownerdraw UI_KEYBINDSTATUS
text " "
forecolor 1 .75 0 1
textscale .25
textalignx 221
textalign ITEM_ALIGN_CENTER
textaligny 20
visible MENU_FALSE
decoration
}

Since this just displays text it is set to decoration to avoid giving it the focus. If the assignment mode of a Key Bind item is on then the text displayed will be "Waiting for new key... Press ESCAPE to cancel". When the assignment mode is off the it displays "Press ENTER or CLICK to change, Press BACKSPACE to clear". Since we don't want either message displayed unless a Key Bind item has the focus we make the keyBindStatus item not visible when the menu is opened.

To make the keyBindStatus item visible when our Key Bind item has the focus and is ready to do a key binding, and invisible otherwise, we use the onFocus and leaveFocus commands.

    itemDef 
    {
name look
type ITEM_TYPE_BIND
text "Lookup:"
cvar "+lookup"
rect 99 50 256 20
textalign ITEM_ALIGN_RIGHT
textalignx 128
textaligny 17
textscale .333
forecolor 1 1 1 1
visible MENU_TRUE onFocus { show keyBindStatus } leaveFocus { hide keyBindStatus }
}

When our Key Bind item receives the focus it executes the onFocus command, which changes the visiblity of the keyBindStatus item so it is drawn. When it loses the focus the leaveFocus command is run, changing the keyBindStatus item's visibility to off. This ensures the status message is only displayed when we want it.

A very nice touch when we are binding keys to action cvars is to display the player model and perform the action on it when we move the mouse over the Key Bind item. The model would revert to an default idle state when the mouse moves off the item.

    itemDef 
    {
      name modelselection
      ownerdraw UI_PLAYERMODEL
      rect 424 80 260 260
      decoration 
      visible MENU_TRUE
    }

This will show the player model. When the menu system is first initialized the player model is set to its default values and displays at in an idle animation. If we return the model back to its default values when the mouse leaves the Key Bind item it will always be ready to be changed by the next item.

Next we have to modify our Key Bind items to change the model to reflect the action of each item. We'll use the onFocus and leaveFocus commands again, since the model change should take place when the item receives and loses the focus.

    itemDef 
    {
name look
type ITEM_TYPE_BIND
text "Lookup:"
cvar "+lookup"
rect 99 50 256 20
textalign ITEM_ALIGN_RIGHT
textalignx 128
textaligny 17
textscale .333
forecolor 1 1 1 1
visible MENU_TRUE onFocus { show keyBindStatus ; setcvarfloat "ui_PlayerViewAnglePitch" -20 ; uiScript UpdateModel } leaveFocus { hide keyBindStatus ; setcvarfloat "ui_PlayerViewAnglePitch" 10 ; uiScript UpdateModel }
}

Here we are modifying the pitch of the view angle when the item receives and looses the focus. A negative pitch angle will make the player model look up so we set it to -20 when the item receives the focus. We restore the angle to the default angle of 10 when the item loses the focus so it is ready to be changed by another item. After each change we update the model using the uiScript command so the change will appear on the displayed model. Note the use of the setcvarfloat command here. Since the angle can be negative we can not use the setcvar command, since it won't accept negative values.

Other Key Bind items, setting other actions, are very similar to the one we've just looked at. Here is one that sets the key used to make the player move forward.

    itemDef 
    {
name move
type ITEM_TYPE_BIND
text "Forward:"
cvar "+forward"
rect 99 100 256 20
textalign ITEM_ALIGN_RIGHT
textalignx 128
textaligny 20
textscale .333
forecolor 1 1 1 1
visible MENU_TRUE
onFocus { show keyBindStatus ; setcvar "ui_LowerAnim" LEGS_WALK ; uiScript UpdateModel }
leaveFocus { hide keyBindStatus ; setcvar "ui_LowerAnim" LEGS_IDLE ; uiScript UpdateModel }
}

Here we are binding a key to the "+forward" cvar, which controls the player's forward movement (walking) in the game. To reflect the action this cvar performs we change the model's lower animation to walking when the item receives the focus. It reverts to idle when the focus is lost. After each change we update the model.

 

Return to Home Page