daviga404
daviga404

Reputation: 538

Getting key name in wxWidgets

In wxWidgets, I'm capturing the wxKeyDown event, which gives me a wxKeyEvent. I need to get the name of the key that was pressed (e.g. "F1", "Del", "Home", "A") to display to the user, however the closest I have found is wxKeyEvent::GetUnicodeKey():

void OnKeyDown(wxKeyEvent &event)
{
    wxMessageBox(wxString::Format("Key pressed: %c", event.GetUnicodeKey()));
}

The only other solution I have found is to use a switch statement with keys that do not have a Unicode representation (e.g. Del). Is there any other way to retrieve the name of the key that was pressed?

Upvotes: 1

Views: 752

Answers (1)

VZ.
VZ.

Reputation: 22678

There is no built-in function to get the name of the key, but you can see how to do it for most (and maybe even all the) special keys in the keyboard sample.

EDIT: Actually there is one function I didn't think of: wxAcceleratorEntry::ToString(). It is rather roundabout but you probably could use wxAcceleratorEntry(0 /* no modifiers */, keycode).ToString() to return a reasonably user-friendly description of the key.

Upvotes: 2

Related Questions