# wxPython 键盘事件列表

wx.KeyEvent

## Type KeyEvent

object --+
|
Object --+
|
Event --+
|
KeyEvent


This event class contains information about keypress and character events. These events are only sent to the widget that currently has the keyboard focus.

Notice that there are three different kinds of keyboard events in wxWidgets: key down and up events and char events. The difference between the first two is clear - the first corresponds to a key press and the second to a key release - otherwise they are identical. Just note that if the key is maintained in a pressed state you will typically get a lot of (automatically generated) down events but only one up so it is wrong to assume that there is one up event corresponding to each down one.

Both key events provide untranslated key codes while the char event carries the translated one. The untranslated code for alphanumeric keys is always an upper case value. For the other keys it is one of WXK_XXX values from the keycodes table. The translated key is, in general, the character the user expects to appear as the result of the key combination when typing the text into a text entry zone, for example.

A few examples to clarify this (all assume that CAPS LOCK is unpressed and the standard US keyboard): when the 'A' key is pressed, the key down event key code is equal to ASCII A == 65. But the char event key code is ASCII a == 97. On the other hand, if you press both SHIFT and 'A' keys simultaneously , the key code in key down event will still be just 'A' while the char event key code parameter will now be 'A' as well.

Although in this simple case it is clear that the correct key code could be found in the key down event handler by checking the value returned by ShiftDown, in general you should use EVT_CHAR for this as for non alphanumeric keys or non-US keyboard layouts the translation is keyboard-layout dependent and can only be done properly by the system itself.

Another kind of translation is done when the control key is pressed: for example, for CTRL-A key press the key down event still carries the same key code 'A' as usual but the char event will have key code of 1, the ASCII value of this key combination.

You may discover how the other keys on your system behave interactively by running the KeyEvents sample in the wxPython demo and pressing some keys while the blue box at the top has the keyboard focus.

Note: If a key down event is caught and the event handler does not call event.Skip() then the coresponding char event will not happen. This is by design and enables the programs that handle both types of events to be a bit simpler.

Note for Windows programmers: The key and char events in wxWidgets are similar to but slightly different from Windows WM_KEYDOWN and WM_CHAR events. In particular, Alt-x combination will generate a char event in wxWidgets (unless it is used as an accelerator).

Tip: be sure to call event.Skip() for events that you don't process in key event function, otherwise menu shortcuts may cease to work under Windows.

# Events

 wx.EVT_KEY_DOWN Sent when a keyboard key has been pressed wx.EVT_KEY_UP Sent when a keyboard key has been released wx.EVT_CHAR Sent for translated character events.

# Keycode Table

Method Summary
KeyEvent __init__(self, eventType)
bool AltDown(self)
Returns True if the Alt key was down at the time of the event.
bool CmdDown(self)
"Cmd" is a pseudo key which is the same as Control for PC and Unix platforms but the special "Apple" (a.k.a as "Command") key on Macs.
bool ControlDown(self)
Returns True if the Control key was down at the time of the event.
int GetKeyCode(self)
Returns the virtual key code.
int GetModifiers(self)
Returns a bitmask of the current modifier settings.
Point GetPosition(self)
Find the position of the event, if applicable.
(x,y) GetPositionTuple()
Find the position of the event, if applicable.
unsigned int GetRawKeyCode(self)
Returns the raw key code for this event.
unsigned int GetRawKeyFlags(self)
Returns the low level key flags for this event.
int GetUniChar(self)
Returns the Unicode character corresponding to this key event.
int GetUnicodeKey(self)
Returns the Unicode character corresponding to this key event.
int GetX(self)
Returns the X position (in client coordinates) of the event, if applicable.
int GetY(self)
Returns the Y position (in client coordinates) of the event, if applicable.
bool HasModifiers(self)
Returns true if either CTRL or ALT keys was down at the time of the key event.
bool MetaDown(self)
Returns True if the Meta key was down at the time of the event.
SetUnicodeKey(self, uniChar)
Set the Unicode value of the key event, but only if this is a Unicode build of wxPython.
bool ShiftDown(self)
Returns True if the Shift key was down at the time of the event.

Property Summary
KeyCode: See GetKeyCode
m_altDown
m_controlDown
m_keyCode
m_metaDown
m_rawCode
m_rawFlags
m_scanCode
m_shiftDown
m_x
m_y
Modifiers: See GetModifiers
Position: See GetPosition
RawKeyCode: See GetRawKeyCode
RawKeyFlags: See GetRawKeyFlags
thisown: The membership flag
UnicodeKey: See GetUnicodeKey and SetUnicodeKey
X: See GetX
Y: See GetY

Method Details

### __init__(self, eventType=wxEVT_NULL)(Constructor)

Construct a new wx.KeyEvent. Valid event types are:
Parameters:
eventType
(type=EventType)

Returns:
KeyEvent
Overrides:
wx.Event.__init__

### AltDown(self)

Returns True if the Alt key was down at the time of the event.

Returns:
bool

### CmdDown(self)

"Cmd" is a pseudo key which is the same as Control for PC and Unix platforms but the special "Apple" (a.k.a as "Command") key on Macs. It makes often sense to use it instead of, say, ControlDown because Cmd key is used for the same thing under Mac as Ctrl elsewhere. The Ctrl still exists, it's just not used for this purpose. So for non-Mac platforms this is the same as ControlDown and Macs this is the same as MetaDown.

Returns:
bool

### ControlDown(self)

Returns True if the Control key was down at the time of the event.

Returns:
bool

### GetKeyCode(self)

Returns the virtual key code. ASCII events return normal ASCII values, while non-ASCII events return values such as WXK_LEFT for the left cursor key. See wx.KeyEvent for a full list of the virtual key codes.

Note that in Unicode build, the returned value is meaningful only if the user entered a character that can be represented in current locale's default charset. You can obtain the corresponding Unicode character using GetUnicodeKey.

Returns:
int

### GetModifiers(self)

Returns a bitmask of the current modifier settings. Can be used to check if the key event has exactly the given modifiers without having to explicitly check that the other modifiers are not down. For example:

if event.GetModifers() == wx.MOD_CONTROL:
DoSomething()

Returns:
int

### GetPosition(self)

Find the position of the event, if applicable.

Returns:
Point

### GetPositionTuple()

Find the position of the event, if applicable.

Returns:
(x,y)

### GetRawKeyCode(self)

Returns the raw key code for this event. This is a platform-dependent scan code which should only be used in advanced applications. Currently the raw key codes are not supported by all ports.

Returns:
unsigned int

### GetRawKeyFlags(self)

Returns the low level key flags for this event. The flags are platform-dependent and should only be used in advanced applications. Currently the raw key flags are not supported by all ports.

Returns:
unsigned int

### GetUniChar(self)

Returns the Unicode character corresponding to this key event. This function is only meaningfule in a Unicode build of wxPython.

Returns:
int

### GetUnicodeKey(self)

Returns the Unicode character corresponding to this key event. This function is only meaningfule in a Unicode build of wxPython.

Returns:
int

### GetX(self)

Returns the X position (in client coordinates) of the event, if applicable.

Returns:
int

### GetY(self)

Returns the Y position (in client coordinates) of the event, if applicable.

Returns:
int

### HasModifiers(self)

Returns true if either CTRL or ALT keys was down at the time of the key event. Note that this function does not take into account neither SHIFT nor META key states (the reason for ignoring the latter is that it is common for NUMLOCK key to be configured as META under X but the key presses even while NUMLOCK is on should be still processed normally).

Returns:
bool

Returns True if the Meta key was down at the time of the event.

Returns:
bool

### SetUnicodeKey(self, uniChar)

Set the Unicode value of the key event, but only if this is a Unicode build of wxPython.

Parameters:
uniChar
(type=int)

### ShiftDown(self)

Returns True if the Shift key was down at the time of the event.

Returns:
bool

Property Details

### KeyCode

Get Method:
GetKeyCode(self)

### Modifiers

Get Method:
GetModifiers(self)

### Position

Get Method:
GetPosition(self)

### RawKeyCode

Get Method:
GetRawKeyCode(self)

### RawKeyFlags

Get Method:
GetRawKeyFlags(self)

### thisown

The membership flag

### UnicodeKey

Get Method:
GetUnicodeKey(self)
Set Method:
SetUnicodeKey(self, uniChar)

### X

Get Method:
GetX(self)

### Y

Get Method:
GetY(self)

 Generated by Epydoc 2.1.20050511.rpd on Mon Feb 16 12:52:31 2009 http://epydoc.sf.net
posted @ 2014-08-11 00:05  阿驹  阅读(1442)  评论(0编辑  收藏