22. Key / mouse shortcuts

We have global key/mouse shortcuts in some global Input_Xxx variables:

  • In CastleSceneManager unit we have Input_Interact and some game operations.
  • In CastlePlayer unit we have PlayerInput_Xxx global variables, these override some camera shortcuts when Player is used.
Global shortcuts are gathered inside InputsAll global map. Thanks to this global map, you can let user to configure game controls, you can detect key conflicts and handle them however you like, etc.

There are also local inputs, local to a specific camera instance, inside Input_Xxx properties of TWalkCamera and TExamineCamera. If you use TPlayer then TPlayer uses it's own TWalkCamera, and overrides (most of) camera inputs with global PlayerInput_Xxx, so (most of) the TWalkCamera.Input_Xxx controls should not be accessed directly, instead change only the global PlayerInput_Xxx. TPlayer sets typical AWSD key controls scheme, and they are also state-sensitive: they change appropriately when player is dead or blocked (see TPlayer.Dead and TPlayer.Blocked).

Every key/mouse shortcut is a TInputShortcut. They are configurable, you can change the shortcuts whenever you want (see TInputShortcut.Assign and other methods). You can also detect conflicts in the keymap and handle them however you like (see GameControlsMenu unit in "The Castle 1" sources for example).

The state of keys on the global keymap (CastleInputs.InputsAll) can be saved/loaded to the config file, if you call InputsAll.SaveToConfig(UserConfig) and InputsAll.LoadFromConfig(UserConfig). See the chapter User preferences for more information about this.

You can also create new TInputShortcut descendants. Then can be global (added to InputsAll, saved to config file and such) or local. See CastleInputs unit API docs. You can easily handle them by overriding TUIControl.Press or TCastleControl.EventPress or TCastleWindow.EventPress methods, or assigning TCastleControl.OnPress or TCastleWindow.OnPress callbacks. You will find then useful methods TInputShortcut.IsEvent(TInputPressRelease) (to detect press/release of input) and TInputShortcut.IsPressed(IUIContainer) (to detect holding (keeping pressed) of input).