KiCad PCB EDA Suite
hotkeys_basic.h File Reference
#include <map>
#include <common.h>
#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 }
 An helper enum for AddHotkeyName function 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 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. More...
 

Functions

int KeyCodeFromKeyName (const wxString &keyname)
 Function KeyCodeFromKeyName return the key code from its user-friendly key name (ie: "Ctrl+M") More...
 
wxString KeyNameFromKeyCode (int aKeycode, bool *aIsFound=nullptr)
 Function KeyNameFromKeyCode 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)
 AddHotkeyName. More...
 
void DisplayHotkeyList (EDA_BASE_FRAME *aFrame, TOOL_MANAGER *aToolMgr)
 Function DisplayHotkeyList Displays the current hotkey list. More...
 
void ReadHotKeyConfig (wxString aFileName, std::map< std::string, int > &aHotKeys)
 Function ReadotKeyConfig Reads a hotkey config file into a map. More...
 
int WriteHotKeyConfig (const std::map< std::string, TOOL_ACTION * > &aActionMap)
 Function WriteHotKeyConfig Updates the hotkeys config file with the hotkeys from the given actions map. More...
 
int ReadLegacyHotkeyConfigFile (const wxString &aFilename, std::map< std::string, int > &aMap)
 Function ReadLegacyHotkeyConfigFile Read hotkey configuration for a given app, possibly before the frame for that app has been created. More...
 
int ReadLegacyHotkeyConfig (const wxString &aAppname, std::map< std::string, int > &aMap)
 Function ReadLegacyHotkeyConfig 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

An helper enum for AddHotkeyName function 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 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 76 of file hotkeys_basic.h.

77 {
78  IS_HOTKEY,
80 };

Function Documentation

◆ AddHotkeyName()

wxString AddHotkeyName ( const wxString &  aText,
int  aHotKey,
HOTKEY_ACTION_TYPE  aStyle 
)

AddHotkeyName.

Parameters
aText- the base text on which to append the hotkey
aHotKey- the hotkey keycode
aStyle- IS_HOTKEY to add <tab><keyname> (shortcuts in menus, same as hotkeys) IS_COMMENT to add <spaces><(keyname)> mainly in tool tips

Definition at line 195 of file hotkeys_basic.cpp.

196 {
197  wxString msg = aText;
198  wxString keyname = KeyNameFromKeyCode( aHotKey );
199 
200  if( !keyname.IsEmpty() )
201  {
202  switch( aStyle )
203  {
204  case IS_HOTKEY:
205  {
206  // Don't add a suffix for unassigned hotkeys:
207  // WX spews debug from wxAcceleratorEntry::ParseAccel if it doesn't
208  // recognise the keyname, which is the case for <unassigned>.
209  if( aHotKey != 0 )
210  {
211  msg << wxT( "\t" ) << keyname;
212  }
213  break;
214  }
215  case IS_COMMENT:
216  {
217  msg << wxT( " (" ) << keyname << wxT( ")" );
218  break;
219  }
220  }
221  }
222 
223 #ifdef USING_MAC_CMD
224  // On OSX, the modifier equivalent to the Ctrl key of PCs
225  // is the Cmd key, but in code we should use Ctrl as prefix in menus
226  msg.Replace( MODIFIER_CMD_MAC, MODIFIER_CTRL_BASE );
227 #endif
228 
229  return msg;
230 }
#define MODIFIER_CTRL_BASE
#define MODIFIER_CMD_MAC
wxString KeyNameFromKeyCode(int aKeycode, bool *aIsFound)
Function KeyNameFromKeyCode return the key name from the key code Only some wxWidgets key values are ...

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 
)

Function DisplayHotkeyList Displays the current hotkey list.

Parameters
aFrame= current active frame
aToolMgr= the tool manager holding the registered actions from which the hotkeys will be harvested

Definition at line 304 of file hotkeys_basic.cpp.

305 {
306  DIALOG_LIST_HOTKEYS dlg( aParent, aToolManager );
307  dlg.ShowModal();
308 }
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)

Function KeyCodeFromKeyName return the key code from its user-friendly key name (ie: "Ctrl+M")

Definition at line 237 of file hotkeys_basic.cpp.

238 {
239  int ii, keycode = KEY_NON_FOUND;
240 
241  // Search for modifiers: Ctrl+ Alt+ and Shift+
242  // Note: on Mac OSX, the Cmd key is equiv here to Ctrl
243  wxString key = keyname;
244  wxString prefix;
245  int modifier = 0;
246 
247  while( true )
248  {
249  prefix.Empty();
250 
251  if( key.StartsWith( MODIFIER_CTRL_BASE ) )
252  {
253  modifier |= MD_CTRL;
254  prefix = MODIFIER_CTRL_BASE;
255  }
256  else if( key.StartsWith( MODIFIER_CMD_MAC ) )
257  {
258  modifier |= MD_CTRL;
259  prefix = MODIFIER_CMD_MAC;
260  }
261  else if( key.StartsWith( MODIFIER_ALT ) )
262  {
263  modifier |= MD_ALT;
264  prefix = MODIFIER_ALT;
265  }
266  else if( key.StartsWith( MODIFIER_SHIFT ) )
267  {
268  modifier |= MD_SHIFT;
269  prefix = MODIFIER_SHIFT;
270  }
271  else
272  {
273  break;
274  }
275 
276  if( !prefix.IsEmpty() )
277  key.Remove( 0, prefix.Len() );
278  }
279 
280  if( (key.length() == 1) && (key[0] > ' ') && (key[0] < 0x7F) )
281  {
282  keycode = key[0];
283  keycode += modifier;
284  return keycode;
285  }
286 
287  for( ii = 0; hotkeyNameList[ii].m_KeyCode != KEY_NON_FOUND; ii++ )
288  {
289  if( key.CmpNoCase( hotkeyNameList[ii].m_Name ) == 0 )
290  {
291  keycode = hotkeyNameList[ii].m_KeyCode + modifier;
292  break;
293  }
294  }
295 
296  return keycode;
297 }
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 
)

Function KeyNameFromKeyCode return the user-friendly key name (ie: "Ctrl+M") from the key code.

Parameters
aKeycode= key code (ascii value, or wxWidgets value for function keys)
aIsFound= a pointer to a bool to return true if found, or false

Function KeyNameFromKeyCode return the user-friendly key name (ie: "Ctrl+M") from the key code.

Parameters
aKeycode= key code (ascii value, or wxWidgets value for function keys)
aIsFound= a 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 138 of file hotkeys_basic.cpp.

139 {
140  wxString keyname, modifier, fullkeyname;
141  int ii;
142  bool found = false;
143 
144  // Assume keycode of 0 is "unassigned"
145  if( (aKeycode & MD_CTRL) != 0 )
146  modifier << MODIFIER_CTRL;
147 
148  if( (aKeycode & MD_ALT) != 0 )
149  modifier << MODIFIER_ALT;
150 
151  if( (aKeycode & MD_SHIFT) != 0 )
152  modifier << MODIFIER_SHIFT;
153 
154  aKeycode &= ~( MD_CTRL | MD_ALT | MD_SHIFT );
155 
156  if( (aKeycode > ' ') && (aKeycode < 0x7F ) )
157  {
158  found = true;
159  keyname.Append( (wxChar)aKeycode );
160  }
161  else
162  {
163  for( ii = 0; ; ii++ )
164  {
165  if( hotkeyNameList[ii].m_KeyCode == KEY_NON_FOUND ) // End of list
166  {
167  keyname = wxT( "<unknown>" );
168  break;
169  }
170 
171  if( hotkeyNameList[ii].m_KeyCode == aKeycode )
172  {
173  keyname = hotkeyNameList[ii].m_Name;
174  found = true;
175  break;
176  }
177  }
178  }
179 
180  if( aIsFound )
181  *aIsFound = found;
182 
183  fullkeyname = modifier + keyname;
184  return fullkeyname;
185 }
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(), HOTKEY_FILTER::FilterMatches(), TOOL_ACTION::GetDescription(), WIDGET_HOTKEY_LIST::ResolveKeyConflicts(), ACTION_MANAGER::RunHotKey(), DIALOG_SET_GRID::TransferDataToWindow(), WIDGET_HOTKEY_LIST::UpdateFromClientData(), and WriteHotKeyConfig().

◆ ReadHotKeyConfig()

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

Function ReadotKeyConfig Reads a hotkey config file into a map.

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

Parameters
aHotKeys

Definition at line 311 of file hotkeys_basic.cpp.

312 {
313  if( fileName.IsEmpty() )
314  {
315  wxFileName fn( "user" );
316  fn.SetExt( DEFAULT_HOTKEY_FILENAME_EXT );
317  fn.SetPath( GetKicadConfigPath() );
318  fileName = fn.GetFullPath();
319  }
320 
321  if( !wxFile::Exists( fileName ) )
322  return;
323 
324  wxFile file( fileName, wxFile::OpenMode::read );
325 
326  if( !file.IsOpened() ) // There is a problem to open file
327  return;
328 
329  wxString input;
330  file.ReadAll( &input );
331  input.Replace( "\r\n", "\n" ); // Convert Windows files to Unix line-ends
332  wxStringTokenizer fileTokenizer( input, "\n", wxTOKEN_STRTOK );
333 
334  while( fileTokenizer.HasMoreTokens() )
335  {
336  wxStringTokenizer lineTokenizer( fileTokenizer.GetNextToken(), "\t" );
337 
338  wxString cmdName = lineTokenizer.GetNextToken();
339  wxString keyName = lineTokenizer.GetNextToken();
340 
341  if( !cmdName.IsEmpty() )
342  aHotKeys[ cmdName.ToStdString() ] = KeyCodeFromKeyName( keyName );
343  }
344 }
int KeyCodeFromKeyName(const wxString &keyname)
Function KeyCodeFromKeyName return the key code from its user-friendly key name (ie: "Ctrl+M")
wxString GetKicadConfigPath()
Return the user configuration path used to store KiCad's configuration files.
Definition: common.cpp:267
#define DEFAULT_HOTKEY_FILENAME_EXT
Definition: hotkeys_basic.h:30

References DEFAULT_HOTKEY_FILENAME_EXT, GetKicadConfigPath(), 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 
)

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

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

Definition at line 375 of file hotkeys_basic.cpp.

376 {
377  // For Eeschema and Pcbnew frames, we read the new combined file.
378  // For other kifaces, we read the frame-based file
379  if( aAppname == LIB_EDIT_FRAME_NAME || aAppname == SCH_EDIT_FRAME_NAME )
380  {
382  }
383  else if( aAppname == PCB_EDIT_FRAME_NAME || aAppname == FOOTPRINT_EDIT_FRAME_NAME )
384  {
386  }
387 
388  return ReadLegacyHotkeyConfigFile( aAppname, aMap );
389 }
int ReadLegacyHotkeyConfigFile(const wxString &aFilename, std::map< std::string, int > &aMap)
Function ReadLegacyHotkeyConfigFile Read hotkey configuration for a given app, possibly before the fr...
#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 
)

Function ReadLegacyHotkeyConfigFile Read hotkey configuration for a given app, possibly before the frame for that app has been created.

Parameters
aFilename= the 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 392 of file hotkeys_basic.cpp.

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

References DEFAULT_HOTKEY_FILENAME_EXT, GetKicadConfigPath(), and KeyCodeFromKeyName().

Referenced by ReadLegacyHotkeyConfig().

◆ WriteHotKeyConfig()

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

Function WriteHotKeyConfig Updates the hotkeys config file with the hotkeys from the given actions map.

Definition at line 347 of file hotkeys_basic.cpp.

348 {
349  std::map<std::string, int> hotkeys;
350  wxFileName fn( "user" );
351 
352  fn.SetExt( DEFAULT_HOTKEY_FILENAME_EXT );
353  fn.SetPath( GetKicadConfigPath() );
354 
355  // Read the existing config (all hotkeys)
356  //
357  ReadHotKeyConfig( fn.GetFullPath(), hotkeys );
358 
359  // Overlay the current app's hotkey definitions onto the map
360  //
361  for( const auto& ii : aActionMap )
362  hotkeys[ ii.first ] = ii.second->GetHotKey();
363 
364  // Write entire hotkey set
365  //
366  wxFile file( fn.GetFullPath(), wxFile::OpenMode::write );
367 
368  for( const auto& ii : hotkeys )
369  file.Write( wxString::Format( "%s\t%s\n", ii.first, KeyNameFromKeyCode( ii.second ) ) );
370 
371  return 1;
372 }
void ReadHotKeyConfig(wxString fileName, std::map< std::string, int > &aHotKeys)
Function ReadotKeyConfig Reads a hotkey config file into a map.
wxString GetKicadConfigPath()
Return the user configuration path used to store KiCad's configuration files.
Definition: common.cpp:267
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
wxString KeyNameFromKeyCode(int aKeycode, bool *aIsFound)
Function KeyNameFromKeyCode return the key name from the key code Only some wxWidgets key values are ...
#define DEFAULT_HOTKEY_FILENAME_EXT
Definition: hotkeys_basic.h:30

References DEFAULT_HOTKEY_FILENAME_EXT, Format(), GetKicadConfigPath(), KeyNameFromKeyCode(), and ReadHotKeyConfig().

Referenced by PANEL_HOTKEYS_EDITOR::TransferDataFromWindow().