Added wxKeyEvent::IsKeyInCategory() method.

This allows to test whether a given key belongs to the category of e.g. arrow
keys or navigation keys in a more concise and more readable manner.

Closes #10268.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@61736 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2009-08-23 00:32:17 +00:00
parent 2e4d0e91bf
commit 7a34307e24
6 changed files with 114 additions and 20 deletions

View File

@@ -394,6 +394,7 @@ All (GUI):
- Added long version field to wxAboutDialogInfo (Jeff Tupper).
- Added wxWindow::CanScroll() behaving like the old HasScrollbar() and made
HasScrollbar() really check for the scrollbar existence.
- Added wxKeyEvent::IsKeyInCategory() (Jeff Tupper).
GTK:

View File

@@ -1504,6 +1504,33 @@ private:
wxEVT_HOTKEY
*/
// key categories: the bit flags for IsKeyInCategory() function
//
// the enum values used may change in future version of wx
// use the named constants only, or bitwise combinations thereof
enum wxKeyCategoryFlags
{
// arrow keys, on and off numeric keypads
WXK_CATEGORY_ARROW = 1,
// page up and page down keys, on and off numeric keypads
WXK_CATEGORY_PAGING = 2,
// home and end keys, on and off numeric keypads
WXK_CATEGORY_JUMP = 4,
// tab key
WXK_CATEGORY_TAB = 8,
// backspace and delete keys, on and off numeric keypads
WXK_CATEGORY_CUT = 16,
// all keys usually used for navigation
WXK_CATEGORY_NAVIGATION = WXK_CATEGORY_ARROW |
WXK_CATEGORY_PAGING |
WXK_CATEGORY_JUMP
};
class WXDLLIMPEXP_CORE wxKeyEvent : public wxEvent,
public wxKeyboardState
{
@@ -1514,6 +1541,9 @@ public:
// get the key code: an ASCII7 char or an element of wxKeyCode enum
int GetKeyCode() const { return (int)m_keyCode; }
// returns true iff this event's key code is of a certain type
bool IsKeyInCategory(int category) const;
#if wxUSE_UNICODE
// get the Unicode character corresponding to this key
wxChar GetUnicodeKey() const { return m_uniChar; }

View File

@@ -1083,6 +1083,36 @@ protected:
};
/**
Flags for categories of keys.
These values are used by wxKeyEvent::IsKeyInCategory(). They may be
combined via the bitwise operators |, &, and ~.
@since 2.9.1
*/
enum wxKeyCategoryFlags
{
/// arrow keys, on and off numeric keypads
WXK_CATEGORY_ARROW,
/// page up and page down keys, on and off numeric keypads
WXK_CATEGORY_PAGING,
/// home and end keys, on and off numeric keypads
WXK_CATEGORY_JUMP,
/// tab key
WXK_CATEGORY_TAB,
/// backspace and delete keys, on and off numeric keypads
WXK_CATEGORY_CUT,
/// union of WXK_CATEGORY_ARROW, WXK_CATEGORY_PAGING, and WXK_CATEGORY_JUMP categories
WXK_CATEGORY_NAVIGATION
};
/**
@class wxKeyEvent
@@ -1177,6 +1207,16 @@ public:
*/
int GetKeyCode() const;
/**
Returns true if the key is in the given key category.
@param category
A bitwise combination of named ::wxKeyCategoryFlags constants.
@since 2.9.1
*/
bool IsKeyInCategory(int category) const;
//@{
/**
Obtains the position (in client coordinates) at which the key was pressed.

View File

@@ -752,6 +752,45 @@ wxKeyEvent::wxKeyEvent(const wxKeyEvent& evt)
#endif
}
bool wxKeyEvent::IsKeyInCategory(int category) const
{
switch ( GetKeyCode() )
{
case WXK_LEFT:
case WXK_RIGHT:
case WXK_UP:
case WXK_DOWN:
case WXK_NUMPAD_LEFT:
case WXK_NUMPAD_RIGHT:
case WXK_NUMPAD_UP:
case WXK_NUMPAD_DOWN:
return (category & WXK_CATEGORY_ARROW) != 0;
case WXK_PAGEDOWN:
case WXK_END:
case WXK_NUMPAD_PAGEUP:
case WXK_NUMPAD_PAGEDOWN:
return (category & WXK_CATEGORY_PAGING) != 0;
case WXK_HOME:
case WXK_PAGEUP:
case WXK_NUMPAD_HOME:
case WXK_NUMPAD_END:
return (category & WXK_CATEGORY_JUMP) != 0;
case WXK_TAB:
return (category & WXK_CATEGORY_TAB) != 0;
case WXK_BACK:
case WXK_DELETE:
case WXK_NUMPAD_DELETE:
return (category & WXK_CATEGORY_CUT) != 0;
default:
return false;
}
}
// ----------------------------------------------------------------------------
// wxWindowCreateEvent
// ----------------------------------------------------------------------------

View File

@@ -473,7 +473,7 @@ void wxTextCtrl::OnChar(wxKeyEvent& event)
return ;
}
if ( !IsEditable() && key != WXK_LEFT && key != WXK_RIGHT && key != WXK_DOWN && key != WXK_UP && key != WXK_TAB &&
if ( !IsEditable() && !event.IsKeyInCategory(WXK_CATEGORY_ARROW | WXK_CATEGORY_TAB) &&
!( key == WXK_RETURN && ( (m_windowStyle & wxTE_PROCESS_ENTER) || (m_windowStyle & wxTE_MULTILINE) ) )
// && key != WXK_PAGEUP && key != WXK_PAGEDOWN && key != WXK_HOME && key != WXK_END
)
@@ -486,8 +486,8 @@ void wxTextCtrl::OnChar(wxKeyEvent& event)
// allow navigation and deletion
GetSelection( &from, &to );
if ( !IsMultiLine() && m_maxLength && GetValue().length() >= m_maxLength &&
key != WXK_LEFT && key != WXK_RIGHT && key != WXK_TAB && key != WXK_UP && key != WXK_DOWN &&
key != WXK_BACK && key != WXK_DELETE && !( key == WXK_RETURN && (m_windowStyle & wxTE_PROCESS_ENTER) ) &&
!event.IsKeyInCategory(WXK_CATEGORY_ARROW | WXK_CATEGORY_TAB | WXK_CATEGORY_CUT) &&
!( key == WXK_RETURN && (m_windowStyle & wxTE_PROCESS_ENTER) ) &&
from == to )
{
// eat it, we don't want to add more than allowed # of characters

View File

@@ -692,23 +692,7 @@ void wxRichTextCtrl::OnChar(wxKeyEvent& event)
if (event.GetEventType() == wxEVT_KEY_DOWN)
{
if (event.GetKeyCode() == WXK_LEFT ||
event.GetKeyCode() == WXK_RIGHT ||
event.GetKeyCode() == WXK_UP ||
event.GetKeyCode() == WXK_DOWN ||
event.GetKeyCode() == WXK_HOME ||
event.GetKeyCode() == WXK_PAGEUP ||
event.GetKeyCode() == WXK_PAGEDOWN ||
event.GetKeyCode() == WXK_END ||
event.GetKeyCode() == WXK_NUMPAD_LEFT ||
event.GetKeyCode() == WXK_NUMPAD_RIGHT ||
event.GetKeyCode() == WXK_NUMPAD_UP ||
event.GetKeyCode() == WXK_NUMPAD_DOWN ||
event.GetKeyCode() == WXK_NUMPAD_HOME ||
event.GetKeyCode() == WXK_NUMPAD_PAGEUP ||
event.GetKeyCode() == WXK_NUMPAD_PAGEDOWN ||
event.GetKeyCode() == WXK_NUMPAD_END)
if (event.IsKeyInCategory(WXK_CATEGORY_NAVIGATION))
{
KeyboardNavigate(event.GetKeyCode(), flags);
return;