Audacity  3.0.3
Keyboard.cpp
Go to the documentation of this file.
1 /**********************************************************************
2 
3  Audacity: A Digital Audio Editor
4 
5  Keyboard.cpp
6 
7  Dominic Mazzoni
8  Brian Gunlogson
9 
10 **********************************************************************/
11 
12 
13 #include "Keyboard.h"
14 
15 #include <wx/event.h>
16 
19 {
20 #if defined(__WXMAC__)
21  wxString newkey;
22  wxString temp = key;
23 
24  // PRL: This is needed to parse older preference files.
25  temp.Replace(wxT("XCtrl+"), wxT("Control+"));
26 
27  // PRL: RawCtrl is the proper replacement for Control, when formatting
28  // wxMenuItem, so that wxWidgets shows ^ in the menu. It is written into
29  // NEW preference files (2.2.0 and later).
30  temp.Replace(wxT("RawCtrl+"), wxT("Control+"));
31  temp.Replace(wxT("Ctrl+"), wxT("Command+"));
32 
33  if (temp.Contains(wxT("Control+"))) {
34  newkey += wxT("RawCtrl+");
35  }
36 
37  if (temp.Contains(wxT("Alt+")) || temp.Contains(wxT("Option+"))) {
38  newkey += wxT("Alt+");
39  }
40 
41  if (temp.Contains(wxT("Shift+"))) {
42  newkey += wxT("Shift+");
43  }
44 
45  if (temp.Contains(wxT("Command+"))) {
46  newkey += wxT("Ctrl+");
47  }
48 
49  (NormalizedKeyStringBase&)*this =
50  newkey + temp.AfterLast(wxT('+'));
51 #else
52  (NormalizedKeyStringBase&)*this = key;
53 #endif
54 }
55 
56 wxString NormalizedKeyString::Display(bool usesSpecialChars) const
57 {
58  (void)usesSpecialChars;//compiler food
59  // using GET to manipulate key string as needed for macOS differences
60  // in displaying of it
61  auto newkey = this->GET();
62 #if defined(__WXMAC__)
63 
64  if (!usesSpecialChars) {
65  // Compose user-visible keystroke names, all ASCII
66  newkey.Replace(wxT("RawCtrl+"), wxT("Control+"));
67  newkey.Replace(wxT("Alt+"), wxT("Option+"));
68  newkey.Replace(wxT("Ctrl+"), wxT("Command+"));
69  }
70  else {
71  // Compose user-visible keystroke names, with special characters
72  newkey.Replace(wxT("Shift+"), wxT("\u21e7"));
73  newkey.Replace(wxT("RawCtrl+"), '^');
74  newkey.Replace(wxT("Alt+"), wxT("\u2325"));
75  newkey.Replace(wxT("Ctrl+"), wxT("\u2318"));
76  }
77 
78 #endif
79 
80  return newkey;
81 }
82 
83 NormalizedKeyString KeyEventToKeyString(const wxKeyEvent & event)
84 {
85  wxString newStr;
86 
87  long key = event.GetKeyCode();
88 
89  if (event.ControlDown())
90  newStr += wxT("Ctrl+");
91 
92  if (event.AltDown())
93  newStr += wxT("Alt+");
94 
95  if (event.ShiftDown())
96  newStr += wxT("Shift+");
97 
98 #if defined(__WXMAC__)
99  if (event.RawControlDown())
100  newStr += wxT("RawCtrl+");
101 #endif
102 
103  if (event.RawControlDown() && key >= 1 && key <= 26)
104  newStr += (wxChar)(64 + key);
105  else if (key >= 33 && key <= 255 && key != 127)
106  newStr += (wxChar)key;
107  else
108  {
109  switch(key)
110  {
111  case WXK_BACK:
112  newStr += wxT("Backspace");
113  break;
114  case WXK_DELETE:
115  newStr += wxT("Delete");
116  break;
117  case WXK_SPACE:
118  newStr += wxT("Space");
119  break;
120  case WXK_TAB:
121  newStr += wxT("Tab");
122  break;
123  case WXK_RETURN:
124  newStr += wxT("Return");
125  break;
126  case WXK_PAGEUP:
127  newStr += wxT("PageUp");
128  break;
129  case WXK_PAGEDOWN:
130  newStr += wxT("PageDown");
131  break;
132  case WXK_END:
133  newStr += wxT("End");
134  break;
135  case WXK_HOME:
136  newStr += wxT("Home");
137  break;
138  case WXK_LEFT:
139  newStr += wxT("Left");
140  break;
141  case WXK_UP:
142  newStr += wxT("Up");
143  break;
144  case WXK_RIGHT:
145  newStr += wxT("Right");
146  break;
147  case WXK_DOWN:
148  newStr += wxT("Down");
149  break;
150  case WXK_ESCAPE:
151  newStr += wxT("Escape");
152  break;
153  case WXK_INSERT:
154  newStr += wxT("Insert");
155  break;
156  case WXK_NUMPAD0:
157  newStr += wxT("NUMPAD0");
158  break;
159  case WXK_NUMPAD1:
160  newStr += wxT("NUMPAD1");
161  break;
162  case WXK_NUMPAD2:
163  newStr += wxT("NUMPAD2");
164  break;
165  case WXK_NUMPAD3:
166  newStr += wxT("NUMPAD3");
167  break;
168  case WXK_NUMPAD4:
169  newStr += wxT("NUMPAD4");
170  break;
171  case WXK_NUMPAD5:
172  newStr += wxT("NUMPAD5");
173  break;
174  case WXK_NUMPAD6:
175  newStr += wxT("NUMPAD6");
176  break;
177  case WXK_NUMPAD7:
178  newStr += wxT("NUMPAD7");
179  break;
180  case WXK_NUMPAD8:
181  newStr += wxT("NUMPAD8");
182  break;
183  case WXK_NUMPAD9:
184  newStr += wxT("NUMPAD9");
185  break;
186  case WXK_MULTIPLY:
187  newStr += wxT("*");
188  break;
189  case WXK_ADD:
190  newStr += wxT("+");
191  break;
192  case WXK_SUBTRACT:
193  newStr += wxT("-");
194  break;
195  case WXK_DECIMAL:
196  newStr += wxT(".");
197  break;
198  case WXK_DIVIDE:
199  newStr += wxT("/");
200  break;
201  case WXK_F1:
202  newStr += wxT("F1");
203  break;
204  case WXK_F2:
205  newStr += wxT("F2");
206  break;
207  case WXK_F3:
208  newStr += wxT("F3");
209  break;
210  case WXK_F4:
211  newStr += wxT("F4");
212  break;
213  case WXK_F5:
214  newStr += wxT("F5");
215  break;
216  case WXK_F6:
217  newStr += wxT("F6");
218  break;
219  case WXK_F7:
220  newStr += wxT("F7");
221  break;
222  case WXK_F8:
223  newStr += wxT("F8");
224  break;
225  case WXK_F9:
226  newStr += wxT("F9");
227  break;
228  case WXK_F10:
229  newStr += wxT("F10");
230  break;
231  case WXK_F11:
232  newStr += wxT("F11");
233  break;
234  case WXK_F12:
235  newStr += wxT("F12");
236  break;
237  case WXK_F13:
238  newStr += wxT("F13");
239  break;
240  case WXK_F14:
241  newStr += wxT("F14");
242  break;
243  case WXK_F15:
244  newStr += wxT("F15");
245  break;
246  case WXK_F16:
247  newStr += wxT("F16");
248  break;
249  case WXK_F17:
250  newStr += wxT("F17");
251  break;
252  case WXK_F18:
253  newStr += wxT("F18");
254  break;
255  case WXK_F19:
256  newStr += wxT("F19");
257  break;
258  case WXK_F20:
259  newStr += wxT("F20");
260  break;
261  case WXK_F21:
262  newStr += wxT("F21");
263  break;
264  case WXK_F22:
265  newStr += wxT("F22");
266  break;
267  case WXK_F23:
268  newStr += wxT("F23");
269  break;
270  case WXK_F24:
271  newStr += wxT("F24");
272  break;
273  case WXK_NUMPAD_ENTER:
274  newStr += wxT("NUMPAD_ENTER");
275  break;
276  case WXK_NUMPAD_F1:
277  newStr += wxT("NUMPAD_F1");
278  break;
279  case WXK_NUMPAD_F2:
280  newStr += wxT("NUMPAD_F2");
281  break;
282  case WXK_NUMPAD_F3:
283  newStr += wxT("NUMPAD_F3");
284  break;
285  case WXK_NUMPAD_F4:
286  newStr += wxT("NUMPAD_F4");
287  break;
288  case WXK_NUMPAD_HOME:
289  newStr += wxT("NUMPAD_HOME");
290  break;
291  case WXK_NUMPAD_LEFT:
292  newStr += wxT("NUMPAD_LEFT");
293  break;
294  case WXK_NUMPAD_UP:
295  newStr += wxT("NUMPAD_UP");
296  break;
297  case WXK_NUMPAD_RIGHT:
298  newStr += wxT("NUMPAD_RIGHT");
299  break;
300  case WXK_NUMPAD_DOWN:
301  newStr += wxT("NUMPAD_DOWN");
302  break;
303  case WXK_NUMPAD_PAGEUP:
304  newStr += wxT("NUMPAD_PAGEUP");
305  break;
306  case WXK_NUMPAD_PAGEDOWN:
307  newStr += wxT("NUMPAD_PAGEDOWN");
308  break;
309  case WXK_NUMPAD_END:
310  newStr += wxT("NUMPAD_END");
311  break;
312  case WXK_NUMPAD_BEGIN:
313  newStr += wxT("NUMPAD_HOME");
314  break;
315  case WXK_NUMPAD_INSERT:
316  newStr += wxT("NUMPAD_INSERT");
317  break;
318  case WXK_NUMPAD_DELETE:
319  newStr += wxT("NUMPAD_DELETE");
320  break;
321  case WXK_NUMPAD_EQUAL:
322  newStr += wxT("NUMPAD_EQUAL");
323  break;
324  case WXK_NUMPAD_MULTIPLY:
325  newStr += wxT("NUMPAD_MULTIPLY");
326  break;
327  case WXK_NUMPAD_ADD:
328  newStr += wxT("NUMPAD_ADD");
329  break;
330  case WXK_NUMPAD_SUBTRACT:
331  newStr += wxT("NUMPAD_SUBTRACT");
332  break;
333  case WXK_NUMPAD_DECIMAL:
334  newStr += wxT("NUMPAD_DECIMAL");
335  break;
336  case WXK_NUMPAD_DIVIDE:
337  newStr += wxT("NUMPAD_DIVIDE");
338  break;
339  default:
340  return {}; // Don't do anything if we don't recognize the key
341  }
342  }
343 
344  return NormalizedKeyString{ newStr };
345 }
NormalizedKeyString::Display
wxString Display(bool usesSpecialChars=false) const
Definition: Keyboard.cpp:56
Identifier::GET
const wxString & GET() const
Explicit conversion to wxString, meant to be ugly-looking and demanding of a comment why it's correct...
Definition: Identifier.h:66
NormalizedKeyString
Definition: Keyboard.h:24
NormalizedKeyString::NormalizedKeyString
NormalizedKeyString()=default
key
static const AudacityProject::AttachedObjects::RegisteredFactory key
Definition: CommandManager.cpp:201
TaggedIdentifier
Template generates different TaggedIdentifier classes that don't interconvert implicitly.
Definition: Identifier.h:113
KeyEventToKeyString
NormalizedKeyString KeyEventToKeyString(const wxKeyEvent &event)
Definition: Keyboard.cpp:83
Keyboard.h