blender/source/gameengine/PyDoc/GameKeys.py
Dalai Felinto 795b438bf5 Patch #21789 - BGE Keyboard and Mouse Python types - by Mitchell Stokes(Moguri)
The patch exposes mouse and keyboard read-only properties in the GameLogic module
Also renames bge.keys to bge.events (* Note: name of bge submodules (logic, render, ...) may change before 2.5 final release [right Campbell?]).

"""
This patch adds two new types to the BGE:
SCA_PythonKeyboard
SCA_PythonMouse

These two types allow users to make use of the keyboard and mouse without the need for a keyboard or mouse sensor.

SCA_PythonKeyboard has an events property that acts just like SCA_KeyboardSensor.events.

SCA_PythonMouse also has an events property to check for mouse events. Further more it supports getting and setting normalized cursor position (from 0.0 to 1.0) with SCA_PythonMouse.position. The cursor can be shown/hidden using SCA_PythonMouse.visible.
"""

Its use is similar with current mouse and keyboard controllers. With the exception of mouse position being normalized and writable as well (replacing Rasterizer.setMousePosition).

Code Sample:
######
from bge import logic, events

mouse = logic.mouse
keyboard = logic.keyboard

for key,status in keyboard.events:
    if status == logic.KX_INPUT_JUST_ACTIVATED:
        if key == events.WKEY:
            print(mouse.position)
            # move_forward()

mouse.visible = True # turn cursor visible
mouse.position = 0.5,0.5  # centralize mouse - use tuple
######

* Important Note: mouse.position still will not work properly for Letterbox mode.
In order to fix letterboxing I may need to move the set x,y mouse function to inside the canvas code (to avoid duplicated code between mouse sensor and bge.logic.mouse). I'll leave this for another commit though.
Thanks Mitchell for the work on that.
2010-04-17 06:52:14 +00:00

184 lines
4.0 KiB
Python

# $Id$
"""
Documentation for the GameKeys module.
======================================
This module holds key constants for the SCA_KeyboardSensor.
Example::
# Set a connected keyboard sensor to accept F1
import GameLogic
import GameKeys
co = GameLogic.getCurrentController()
# 'Keyboard' is a keyboard sensor
sensor = co.getSensor('Keyboard')
sensor.key = GameKeys.F1KEY
Example::
# Do the all keys thing
import GameLogic
import GameKeys
co = GameLogic.getCurrentController()
# 'Keyboard' is a keyboard sensor
sensor = co.getSensor('Keyboard')
keylist = sensor.events
for key in keylist:
# key[0] == GameKeys.keycode, key[1] = status
if key[1] == GameLogic.KX_INPUT_JUST_ACTIVATED:
if key[0] == GameKeys.WKEY:
# Activate Forward!
if key[0] == GameKeys.SKEY:
# Activate Backward!
if key[0] == GameKeys.AKEY:
# Activate Left!
if key[0] == GameKeys.DKEY:
# Activate Right!
@group Alphabet keys: AKEY, BKEY, CKEY, DKEY, EKEY, FKEY, GKEY, HKEY, IKEY, JKEY, KKEY, LKEY, MKEY, NKEY, OKEY, PKEY, QKEY, RKEY, SKEY, TKEY, UKEY, VKEY, WKEY, XKEY, YKEY, ZKEY
@var AKEY:
@var BKEY:
@var CKEY:
@var DKEY:
@var EKEY:
@var FKEY:
@var GKEY:
@var HKEY:
@var IKEY:
@var JKEY:
@var KKEY:
@var LKEY:
@var MKEY:
@var NKEY:
@var OKEY:
@var PKEY:
@var QKEY:
@var RKEY:
@var SKEY:
@var TKEY:
@var UKEY:
@var VKEY:
@var WKEY:
@var XKEY:
@var YKEY:
@var ZKEY:
@group Number keys: ZEROKEY, ONEKEY, TWOKEY, THREEKEY, FOURKEY, FIVEKEY, SIXKEY, SEVENKEY, EIGHTKEY, NINEKEY
@var ZEROKEY:
@var ONEKEY:
@var TWOKEY:
@var THREEKEY:
@var FOURKEY:
@var FIVEKEY:
@var SIXKEY:
@var SEVENKEY:
@var EIGHTKEY:
@var NINEKEY:
@group Modifiers: CAPSLOCKKEY, LEFTCTRLKEY, LEFTALTKEY, RIGHTALTKEY, RIGHTCTRLKEY, RIGHTSHIFTKEY, LEFTSHIFTKEY
@var CAPSLOCKKEY:
@var LEFTCTRLKEY:
@var LEFTALTKEY:
@var RIGHTALTKEY:
@var RIGHTCTRLKEY:
@var RIGHTSHIFTKEY:
@var LEFTSHIFTKEY:
@group Arrow Keys: LEFTARROWKEY, DOWNARROWKEY, RIGHTARROWKEY, UPARROWKEY
@var LEFTARROWKEY:
@var DOWNARROWKEY:
@var RIGHTARROWKEY:
@var UPARROWKEY:
@group Numberpad Keys: PAD0, PAD1, PAD2, PAD3, PAD4, PAD5, PAD6, PAD7, PAD8, PAD9, PADPERIOD, PADSLASHKEY, PADASTERKEY, PADMINUS, PADENTER, PADPLUSKEY
@var PAD0:
@var PAD1:
@var PAD2:
@var PAD3:
@var PAD4:
@var PAD5:
@var PAD6:
@var PAD7:
@var PAD8:
@var PAD9:
@var PADPERIOD:
@var PADSLASHKEY:
@var PADASTERKEY:
@var PADMINUS:
@var PADENTER:
@var PADPLUSKEY:
@group Function Keys: F1KEY, F2KEY, F3KEY, F4KEY, F5KEY, F6KEY, F7KEY, F8KEY, F9KEY, F10KEY, F11KEY, F12KEY
@var F1KEY:
@var F2KEY:
@var F3KEY:
@var F4KEY:
@var F5KEY:
@var F6KEY:
@var F7KEY:
@var F8KEY:
@var F9KEY:
@var F10KEY:
@var F11KEY:
@var F12KEY:
@group Other Keys: ACCENTGRAVEKEY, BACKSLASHKEY, BACKSPACEKEY, COMMAKEY, DELKEY, ENDKEY, EQUALKEY, ESCKEY, HOMEKEY, INSERTKEY, LEFTBRACKETKEY, LINEFEEDKEY, MINUSKEY, PAGEDOWNKEY, PAGEUPKEY, PAUSEKEY, PERIODKEY, QUOTEKEY, RIGHTBRACKETKEY, RETKEY, SEMICOLONKEY, SLASHKEY, SPACEKEY, TABKEY
@var ACCENTGRAVEKEY:
@var BACKSLASHKEY:
@var BACKSPACEKEY:
@var COMMAKEY:
@var DELKEY:
@var ENDKEY:
@var EQUALKEY:
@var ESCKEY:
@var HOMEKEY:
@var INSERTKEY:
@var LEFTBRACKETKEY:
@var LINEFEEDKEY:
@var MINUSKEY:
@var PAGEDOWNKEY:
@var PAGEUPKEY:
@var PAUSEKEY:
@var PERIODKEY:
@var QUOTEKEY:
@var RIGHTBRACKETKEY:
@var RETKEY:
@var SEMICOLONKEY:
@var SLASHKEY:
@var SPACEKEY:
@var TABKEY:
@group Mouse Events: LEFTMOUSE, MIDDLEMOUSE, RIGHTMOUSE, WHEELUPMOUSE, WHEELDOWNMOUSE, MOUSEX, MOUSEY
@var LEFTMOUSE:
@var MIDDLEMOUSE:
@var RIGHTMOUSE:
@var WHEELUPMOUSE:
@var WHEELDOWNMOUSE:
@var MOUSEX:
@var MOUSEY:
"""
def EventToString(event):
"""
Return the string name of a key event. Will raise a ValueError error if its invalid.
@type event: int
@param event: key event from GameKeys or the keyboard sensor.
@rtype: string
"""
def EventToCharacter(event, shift):
"""
Return the string name of a key event. Returns an empty string if the event cant be represented as a character.
@type event: int
@param event: key event from GameKeys or the keyboard sensor.
@type shift: bool
@param shift: set to true if shift is held.
@rtype: string
"""