KiCad PCB EDA Suite
hotkeys_basic.h File Reference
#include <wx/string.h>
#include <map>
#include <i18n_utility.h>

Go to the source code of this file.

Macros

#define DEFAULT_HOTKEY_FILENAME_EXT   wxT( "hotkeys" )
 
#define EESCHEMA_HOTKEY_NAME   wxT( "Eeschema" )
 
#define PCBNEW_HOTKEY_NAME   wxT( "PcbNew" )
 
#define PSEUDO_WXK_CLICK   400
 
#define PSEUDO_WXK_DBLCLICK   401
 
#define PSEUDO_WXK_WHEEL   402
 

Enumerations

enum  HOTKEY_ACTION_TYPE { IS_HOTKEY, IS_COMMENT }
 In menus we can add a hot key, or an accelerator, or sometimes just a comment. More...
 

Functions

int KeyCodeFromKeyName (const wxString &keyname)
 Return the key code from its user-friendly key name (ie: "Ctrl+M"). More...
 
wxString KeyNameFromKeyCode (int aKeycode, bool *aIsFound=nullptr)
 Return the user friendly key name (ie: "Ctrl+M") from the key code. More...
 
wxString AddHotkeyName (const wxString &aText, int aHotKey, HOTKEY_ACTION_TYPE aStyle=IS_HOTKEY)
 
void DisplayHotkeyList (EDA_BASE_FRAME *aFrame, TOOL_MANAGER *aToolMgr)
 Display the current hotkey list. More...
 
void ReadHotKeyConfig (const wxString &aFileName, std::map< std::string, int > &aHotKeys)
 Reads a hotkey config file into a map. More...
 
int WriteHotKeyConfig (const std::map< std::string, TOOL_ACTION * > &aActionMap)
 Update the hotkeys config file with the hotkeys from the given actions map. More...
 
int ReadLegacyHotkeyConfigFile (const wxString &aFilename, std::map< std::string, int > &aMap)
 Read hotkey configuration for a given app. More...
 
int ReadLegacyHotkeyConfig (const wxString &aAppname, std::map< std::string, int > &aMap)
 Read configuration data and fill the current hotkey list with hotkeys. More...
 

Macro Definition Documentation

◆ DEFAULT_HOTKEY_FILENAME_EXT

#define DEFAULT_HOTKEY_FILENAME_EXT   wxT( "hotkeys" )

Definition at line 30 of file hotkeys_basic.h.

◆ EESCHEMA_HOTKEY_NAME

#define EESCHEMA_HOTKEY_NAME   wxT( "Eeschema" )

Definition at line 31 of file hotkeys_basic.h.

◆ PCBNEW_HOTKEY_NAME

#define PCBNEW_HOTKEY_NAME   wxT( "PcbNew" )

Definition at line 32 of file hotkeys_basic.h.

◆ PSEUDO_WXK_CLICK

#define PSEUDO_WXK_CLICK   400

Definition at line 50 of file hotkeys_basic.h.

◆ PSEUDO_WXK_DBLCLICK

#define PSEUDO_WXK_DBLCLICK   401

Definition at line 51 of file hotkeys_basic.h.

◆ PSEUDO_WXK_WHEEL

#define PSEUDO_WXK_WHEEL   402

Definition at line 52 of file hotkeys_basic.h.

Enumeration Type Documentation

◆ HOTKEY_ACTION_TYPE

In menus we can add a hot key, or an accelerator, or sometimes just a comment.

Hot keys can perform actions using the current mouse cursor position and accelerators perform the same action as the associated menu.

A comment is used in tool tips for some tools (zoom ..) to show the hot key that performs this action

Enumerator
IS_HOTKEY 
IS_COMMENT 

Definition at line 75 of file hotkeys_basic.h.

76 {
77  IS_HOTKEY,
79 };

Function Documentation

◆ AddHotkeyName()

wxString AddHotkeyName ( const wxString &  aText,
int  aHotKey,
HOTKEY_ACTION_TYPE  aStyle 
)
Parameters
aTextthe base text on which to append the hotkey.
aHotKeythe hotkey keycode.
aStyleIS_HOTKEY to add <tab><keyname> (shortcuts in menus, same as hotkeys). IS_COMMENT to add <spaces><(keyname)> mainly in tool tips.
aTextthe base text on which to append the hotkey.
aHotKeythe hotkey keycode.
aStyleIS_HOTKEY to add <tab><keyname> (shortcuts in menus, same as hotkeys). IS_COMMENT to add <spaces><(keyname)> mainly in tool tips.

Definition at line 208 of file hotkeys_basic.cpp.

209 {
210  wxString msg = aText;
211  wxString keyname = KeyNameFromKeyCode( aHotKey );
212 
213  if( !keyname.IsEmpty() )
214  {
215  switch( aStyle )
216  {
217  case IS_HOTKEY:
218  {
219  // Don't add a suffix for unassigned hotkeys:
220  // WX spews debug from wxAcceleratorEntry::ParseAccel if it doesn't
221  // recognize the keyname, which is the case for <unassigned>.
222  if( aHotKey != 0 )
223  {
224  msg << wxT( "\t" ) << keyname;
225  }
226  break;
227  }
228  case IS_COMMENT:
229  {
230  msg << wxT( " (" ) << keyname << wxT( ")" );
231  break;
232  }
233  }
234  }
235 
236 #ifdef USING_MAC_CMD
237  // On OSX, the modifier equivalent to the Ctrl key of PCs
238  // is the Cmd key, but in code we should use Ctrl as prefix in menus
239  msg.Replace( MODIFIER_CMD_MAC, MODIFIER_CTRL_BASE );
240 #endif
241 
242  return msg;
243 }
#define MODIFIER_CTRL_BASE
#define MODIFIER_CMD_MAC
wxString KeyNameFromKeyCode(int aKeycode, bool *aIsFound)
Return the key name from the key code.

References IS_COMMENT, IS_HOTKEY, KeyNameFromKeyCode(), MODIFIER_CMD_MAC, and MODIFIER_CTRL_BASE.

Referenced by TOOL_ACTION::GetMenuItem(), and PCB_LAYER_BOX_SELECTOR::Resync().

◆ DisplayHotkeyList()

void DisplayHotkeyList ( EDA_BASE_FRAME aFrame,
TOOL_MANAGER aToolMgr 
)

Display the current hotkey list.

Parameters
aFramecurrent active frame.
aToolMgrthe tool manager holding the registered actions from which the hotkeys will be harvested.

Definition at line 315 of file hotkeys_basic.cpp.

316 {
317  DIALOG_LIST_HOTKEYS dlg( aParent, aToolManager );
318  dlg.ShowModal();
319 }
A dialog that presents the user with a read-only list of hotkeys and their current bindings.

Referenced by COMMON_CONTROL::ListHotKeys().

◆ KeyCodeFromKeyName()

int KeyCodeFromKeyName ( const wxString &  keyname)

Return the key code from its user-friendly key name (ie: "Ctrl+M").

Definition at line 249 of file hotkeys_basic.cpp.

250 {
251  int ii, keycode = KEY_NON_FOUND;
252 
253  // Search for modifiers: Ctrl+ Alt+ and Shift+
254  // Note: on Mac OSX, the Cmd key is equiv here to Ctrl
255  wxString key = keyname;
256  wxString prefix;
257  int modifier = 0;
258 
259  while( true )
260  {
261  prefix.Empty();
262 
263  if( key.StartsWith( MODIFIER_CTRL_BASE ) )
264  {
265  modifier |= MD_CTRL;
266  prefix = MODIFIER_CTRL_BASE;
267  }
268  else if( key.StartsWith( MODIFIER_CMD_MAC ) )
269  {
270  modifier |= MD_CTRL;
271  prefix = MODIFIER_CMD_MAC;
272  }
273  else if( key.StartsWith( MODIFIER_ALT ) )
274  {
275  modifier |= MD_ALT;
276  prefix = MODIFIER_ALT;
277  }
278  else if( key.StartsWith( MODIFIER_SHIFT ) )
279  {
280  modifier |= MD_SHIFT;
281  prefix = MODIFIER_SHIFT;
282  }
283  else
284  {
285  break;
286  }
287 
288  if( !prefix.IsEmpty() )
289  key.Remove( 0, prefix.Len() );
290  }
291 
292  if( (key.length() == 1) && (key[0] > ' ') && (key[0] < 0x7F) )
293  {
294  keycode = key[0];
295  keycode += modifier;
296  return keycode;
297  }
298 
299  for( ii = 0; hotkeyNameList[ii].m_KeyCode != KEY_NON_FOUND; ii++ )
300  {
301  if( key.CmpNoCase( hotkeyNameList[ii].m_Name ) == 0 )
302  {
303  keycode = hotkeyNameList[ii].m_KeyCode + modifier;
304  break;
305  }
306  }
307 
308  return keycode;
309 }
const wxChar * m_Name
#define MODIFIER_CTRL_BASE
#define KEY_NON_FOUND
#define MODIFIER_SHIFT
#define MODIFIER_ALT
#define MODIFIER_CMD_MAC
static struct hotkey_name_descr hotkeyNameList[]

References hotkeyNameList, KEY_NON_FOUND, hotkey_name_descr::m_KeyCode, hotkey_name_descr::m_Name, MD_ALT, MD_CTRL, MD_SHIFT, MODIFIER_ALT, MODIFIER_CMD_MAC, MODIFIER_CTRL_BASE, and MODIFIER_SHIFT.

Referenced by ReadHotKeyConfig(), and ReadLegacyHotkeyConfigFile().

◆ KeyNameFromKeyCode()

wxString KeyNameFromKeyCode ( int  aKeycode,
bool *  aIsFound 
)

Return the user friendly key name (ie: "Ctrl+M") from the key code.

Parameters
aKeycodekey code (ASCII value, or wxWidgets value for function keys).
aIsFounda pointer to a bool to return true if found, or false.

Return the user friendly key name (ie: "Ctrl+M") from the key code.

Only some wxWidgets key values are handled for function key ( see hotkeyNameList[] )

Parameters
aKeycodekey code (ASCII value, or wxWidgets value for function keys).
aIsFounda pointer to a bool to return true if found, or false. an be nullptr default).
Returns
the key name in a wxString.

Definition at line 152 of file hotkeys_basic.cpp.

153 {
154  wxString keyname, modifier, fullkeyname;
155  int ii;
156  bool found = false;
157 
158  // Assume keycode of 0 is "unassigned"
159  if( (aKeycode & MD_CTRL) != 0 )
160  modifier << MODIFIER_CTRL;
161 
162  if( (aKeycode & MD_ALT) != 0 )
163  modifier << MODIFIER_ALT;
164 
165  if( (aKeycode & MD_SHIFT) != 0 )
166  modifier << MODIFIER_SHIFT;
167 
168  aKeycode &= ~( MD_CTRL | MD_ALT | MD_SHIFT );
169 
170  if( (aKeycode > ' ') && (aKeycode < 0x7F ) )
171  {
172  found = true;
173  keyname.Append( (wxChar)aKeycode );
174  }
175  else
176  {
177  for( ii = 0; ; ii++ )
178  {
179  if( hotkeyNameList[ii].m_KeyCode == KEY_NON_FOUND ) // End of list
180  {
181  keyname = wxT( "<unknown>" );
182  break;
183  }
184 
185  if( hotkeyNameList[ii].m_KeyCode == aKeycode )
186  {
187  keyname = hotkeyNameList[ii].m_Name;
188  found = true;
189  break;
190  }
191  }
192  }
193 
194  if( aIsFound )
195  *aIsFound = found;
196 
197  fullkeyname = modifier + keyname;
198  return fullkeyname;
199 }
const wxChar * m_Name
#define KEY_NON_FOUND
#define MODIFIER_CTRL
#define MODIFIER_SHIFT
#define MODIFIER_ALT
static struct hotkey_name_descr hotkeyNameList[]

References hotkeyNameList, KEY_NON_FOUND, hotkey_name_descr::m_KeyCode, hotkey_name_descr::m_Name, MD_ALT, MD_CTRL, MD_SHIFT, MODIFIER_ALT, MODIFIER_CTRL, and MODIFIER_SHIFT.

Referenced by AddHotkeyName(), WIDGET_HOTKEY_LIST::changeHotkey(), PANEL_HOTKEYS_EDITOR::dumpHotkeys(), PAD_TOOL::EditPad(), HOTKEY_FILTER::FilterMatches(), TOOL_ACTION::GetDescription(), WIDGET_HOTKEY_LIST::resolveKeyConflicts(), ACTION_MANAGER::RunHotKey(), DIALOG_GRID_SETTINGS::TransferDataToWindow(), WIDGET_HOTKEY_LIST::updateFromClientData(), and WriteHotKeyConfig().

◆ ReadHotKeyConfig()

void ReadHotKeyConfig ( const wxString &  aFileName,
std::map< std::string, int > &  aHotKeys 
)

Reads a hotkey config file into a map.

If aFileName is empty it will read in the default hotkeys file.

Definition at line 322 of file hotkeys_basic.cpp.

323 {
324  wxString fileName = aFileName;
325 
326  if( fileName.IsEmpty() )
327  {
328  wxFileName fn( "user" );
329  fn.SetExt( DEFAULT_HOTKEY_FILENAME_EXT );
331  fileName = fn.GetFullPath();
332  }
333 
334  if( !wxFile::Exists( fileName ) )
335  return;
336 
337  wxFFile file( fileName, "rb" );
338 
339  if( !file.IsOpened() ) // There is a problem to open file
340  return;
341 
342  wxString input;
343  file.ReadAll( &input );
344  input.Replace( "\r\n", "\n" ); // Convert Windows files to Unix line-ends
345  wxStringTokenizer fileTokenizer( input, "\n", wxTOKEN_STRTOK );
346 
347  while( fileTokenizer.HasMoreTokens() )
348  {
349  wxStringTokenizer lineTokenizer( fileTokenizer.GetNextToken(), "\t" );
350 
351  wxString cmdName = lineTokenizer.GetNextToken();
352  wxString keyName = lineTokenizer.GetNextToken();
353 
354  if( !cmdName.IsEmpty() )
355  aHotKeys[ cmdName.ToStdString() ] = KeyCodeFromKeyName( keyName );
356  }
357 }
int KeyCodeFromKeyName(const wxString &keyname)
Return the key code from its user-friendly key name (ie: "Ctrl+M").
static wxString GetUserSettingsPath()
Return the user configuration path used to store KiCad's configuration files.
#define DEFAULT_HOTKEY_FILENAME_EXT
Definition: hotkeys_basic.h:30

References DEFAULT_HOTKEY_FILENAME_EXT, SETTINGS_MANAGER::GetUserSettingsPath(), and KeyCodeFromKeyName().

Referenced by PANEL_HOTKEYS_EDITOR::ImportHotKeys(), ACTION_MANAGER::UpdateHotKeys(), and WriteHotKeyConfig().

◆ ReadLegacyHotkeyConfig()

int ReadLegacyHotkeyConfig ( const wxString &  aAppname,
std::map< std::string, int > &  aMap 
)

Read configuration data and fill the current hotkey list with hotkeys.

Parameters
aAppnamethe value of the app's m_FrameName.
aMapThe list of keycodes mapped by legacy property names.

Definition at line 390 of file hotkeys_basic.cpp.

391 {
392  // For Eeschema and Pcbnew frames, we read the new combined file.
393  // For other kifaces, we read the frame-based file
394  if( aAppname == LIB_EDIT_FRAME_NAME || aAppname == SCH_EDIT_FRAME_NAME )
395  {
397  }
398  else if( aAppname == PCB_EDIT_FRAME_NAME || aAppname == FOOTPRINT_EDIT_FRAME_NAME )
399  {
401  }
402 
403  return ReadLegacyHotkeyConfigFile( aAppname, aMap );
404 }
int ReadLegacyHotkeyConfigFile(const wxString &aFilename, std::map< std::string, int > &aMap)
Read hotkey configuration for a given app.
#define LIB_EDIT_FRAME_NAME
#define SCH_EDIT_FRAME_NAME
#define FOOTPRINT_EDIT_FRAME_NAME
#define EESCHEMA_HOTKEY_NAME
Definition: hotkeys_basic.h:31
#define PCB_EDIT_FRAME_NAME
#define PCBNEW_HOTKEY_NAME
Definition: hotkeys_basic.h:32

References EESCHEMA_HOTKEY_NAME, FOOTPRINT_EDIT_FRAME_NAME, LIB_EDIT_FRAME_NAME, PCB_EDIT_FRAME_NAME, PCBNEW_HOTKEY_NAME, ReadLegacyHotkeyConfigFile(), and SCH_EDIT_FRAME_NAME.

Referenced by ACTION_MANAGER::UpdateHotKeys().

◆ ReadLegacyHotkeyConfigFile()

int ReadLegacyHotkeyConfigFile ( const wxString &  aFilename,
std::map< std::string, int > &  aMap 
)

Read hotkey configuration for a given app.

Parameters
aFilenamethe filename to save the hotkeys as.
aMapThe list of keycodes mapped by legacy property names.
Returns
1 on success, 0 on failure.

Definition at line 407 of file hotkeys_basic.cpp.

408 {
409  wxFileName fn( aFilename );
410 
411  fn.SetExt( DEFAULT_HOTKEY_FILENAME_EXT );
413 
414  if( !wxFile::Exists( fn.GetFullPath() ) )
415  return 0;
416 
417  wxFFile cfgfile( fn.GetFullPath(), "rb" );
418 
419  if( !cfgfile.IsOpened() ) // There is a problem to open file
420  return 0;
421 
422  // get length
423  wxFileOffset size = cfgfile.Length();
424 
425  // read data
426  std::vector<char> buffer( size );
427  cfgfile.Read( buffer.data(), size );
428  wxString data( buffer.data(), wxConvUTF8, size );
429 
430  // Is this the wxConfig format? If so, remove "Keys=" and parse the newlines.
431  if( data.StartsWith( wxT("Keys="), &data ) )
432  data.Replace( "\\n", "\n", true );
433 
434  // parse
435  wxStringTokenizer tokenizer( data, L"\r\n", wxTOKEN_STRTOK );
436 
437  while( tokenizer.HasMoreTokens() )
438  {
439  wxString line = tokenizer.GetNextToken();
440  wxStringTokenizer lineTokenizer( line );
441 
442  wxString line_type = lineTokenizer.GetNextToken();
443 
444  if( line_type[0] == '#' ) // comment
445  continue;
446 
447  if( line_type[0] == '[' ) // tags ignored reading legacy hotkeys
448  continue;
449 
450  if( line_type == wxT( "$Endlist" ) )
451  break;
452 
453  if( line_type != wxT( "shortcut" ) )
454  continue;
455 
456  // Get the key name
457  lineTokenizer.SetString( lineTokenizer.GetString(), L"\"\r\n\t ", wxTOKEN_STRTOK );
458  wxString keyname = lineTokenizer.GetNextToken();
459 
460  wxString remainder = lineTokenizer.GetString();
461 
462  // Get the command name
463  wxString fctname = remainder.AfterFirst( '\"' ).BeforeFirst( '\"' );
464 
465  // Add the pair to the map
466  aMap[ fctname.ToStdString() ] = KeyCodeFromKeyName( keyname );
467  }
468 
469  // cleanup
470  cfgfile.Close();
471  return 1;
472 }
int KeyCodeFromKeyName(const wxString &keyname)
Return the key code from its user-friendly key name (ie: "Ctrl+M").
static wxString GetUserSettingsPath()
Return the user configuration path used to store KiCad's configuration files.
#define DEFAULT_HOTKEY_FILENAME_EXT
Definition: hotkeys_basic.h:30

References DEFAULT_HOTKEY_FILENAME_EXT, SETTINGS_MANAGER::GetUserSettingsPath(), and KeyCodeFromKeyName().

Referenced by ReadLegacyHotkeyConfig().

◆ WriteHotKeyConfig()

int WriteHotKeyConfig ( const std::map< std::string, TOOL_ACTION * > &  aActionMap)

Update the hotkeys config file with the hotkeys from the given actions map.

Definition at line 360 of file hotkeys_basic.cpp.

361 {
362  std::map<std::string, int> hotkeys;
363  wxFileName fn( "user" );
364 
365  fn.SetExt( DEFAULT_HOTKEY_FILENAME_EXT );
367 
368  // Read the existing config (all hotkeys)
369  ReadHotKeyConfig( fn.GetFullPath(), hotkeys );
370 
371  // Overlay the current app's hotkey definitions onto the map
372  for( const auto& ii : aActionMap )
373  hotkeys[ ii.first ] = ii.second->GetHotKey();
374 
375  // Write entire hotkey set
376  wxFFileOutputStream outStream( fn.GetFullPath() );
377  wxTextOutputStream txtStream( outStream, wxEOL_UNIX );
378 
379  for( const auto& ii : hotkeys )
380  txtStream << wxString::Format( "%s\t%s", ii.first,
381  KeyNameFromKeyCode( ii.second ) ) << endl;
382 
383  txtStream.Flush();
384  outStream.Close();
385 
386  return 1;
387 }
void ReadHotKeyConfig(const wxString &aFileName, std::map< std::string, int > &aHotKeys)
Reads a hotkey config file into a map.
static wxString GetUserSettingsPath()
Return the user configuration path used to store KiCad's configuration files.
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
wxString KeyNameFromKeyCode(int aKeycode, bool *aIsFound)
Return the key name from the key code.
#define DEFAULT_HOTKEY_FILENAME_EXT
Definition: hotkeys_basic.h:30

References DEFAULT_HOTKEY_FILENAME_EXT, Format(), SETTINGS_MANAGER::GetUserSettingsPath(), hotkeys, KeyNameFromKeyCode(), and ReadHotKeyConfig().

Referenced by PANEL_HOTKEYS_EDITOR::TransferDataFromWindow().