How to install custom keyboard shortcuts

edited August 2009 in KLayout Support

General

I frequently receive requests about customizing the keyboard shortcuts. Although there is no nice dialog yet to do this, there is a way to configure menu shortcuts.

Basically this is possible with the ruby language built into KLayout (provided it was compiled with that option) with a piece of code like this:

# a convenience function to specify a shortcut for a given menu
def set_shortcut(path, shortcut)
  menu = RBA::Application.instance.main_window.menu
  if menu.is_valid(path)
    menu.action(path).shortcut = shortcut
  end
end

# set the shortcut
set_shortcut("file_menu.new_layout", "Ctrl+N")

The last line in that code installs a keyboard shortcut of "Ctrl+N" for the "New Layout" function in the "File" menu.

To install this procedure, paste this code into a file and copy the file to the installation directory (the directory where the KLayout executable is stored).

Name the file as follows:

  • "rbainit" (without extension) for KLayout versions <0.18
  • "my_shortcuts.rbm" (for example) for versions >=0.18

Finding the right identifiers

The basic problem is to find the identification string for the menu items ("file_menu.new_layout" in the above example).

Fortunately we can employ Ruby for that purpose: just copy the following code into a file, i.e. "list_menu.rb":

def list_items( menu, parent )
  menu.items( parent ).each do |item|
    title = menu.action( item ).title
    if title != "" && ! menu.is_menu( item )
      puts "# '#{title}'"
      shortcut = menu.action( item ).shortcut
      puts "set_shortcut(\"#{item}\", \"#{shortcut}\")"
    end
    list_items( menu, item )
  end
end

list_items(RBA::Application.instance.main_window.menu, "")

Then run the code, i.e. by starting KLayout in the right mode (usually editor mode if you are interested in all menu items), open the RBA console (in the "Tools" menu) and enter this command in the direct command line edit box at the bottom:

load("list_menu.rb")

If you need to specify a path on Windows, be sure to escape any backslashes, i.e.

load("c:\\users\\me\\list_menu.rb")

The code will print a list of all menu items with their current shortcut, i.e.

# 'Exit'
set_shortcut("file_menu.exit", "Ctrl+Q")

This commands can be used in the keyboard shortcut customization file.

Special items

Some items occure several times, i.e. the "Move" mode selection. It appears once in the "Mode" submenu of the "Edit" menu ("edit_menu.mode_menu.move"), and another time on the toolbar ("@toolbar.move").

To make sure, the shortcut is set properly, it is important to specify the shortcut for both, i.e.

set_shortcut("edit_menu.mode_menu.move", "M")
set_shortcut("@toolbar.move", "M")

Comments

  • JayJay
    edited November -1
    Very helpful.

    In 0.19.1 "Key Bindings" tab of File => Setup works well. I was able to map my current editor keys to klayout.

    Watch out when defining "<" and ">" as keys. It seems to conflict with XML format of init file. I ended up using "Shift+," and "Shift+.". This works nicely.
  • edited November -1

    Hi Jay,

    I seem not to be able to reproduce the problem with the init file. In my case the < and > keys are mapped to < and > entities in the init file.

    I am building on Ubuntu 9.10.

    But I am taking that serious. Is it possible to send the line from the init file ($HOME/.layviewrc) containing the "" element?

    Best regards,

    Matthias

  • JayJay
    edited December 2009

    Matthias,

    In my case, it never writes/updates the .layviewrc file, when I use "<" and ">" for changing layout depth. I was on Fedora 7 with QT4.

    I will update you, as I try it on Fedora 9/10, CentOS 5.x and RHEL 3.x, 4.x, 5.x, etc.

    ~J

  • edited November -1

    Hi Jay,

    that is really strange. The reason why I am asking for Qt is that the keyboard shortcuts are interpreted by Qt. Maybe the Qt version on Fedora 7 does not like these shortcuts and produces invalid results when the shortcut strings are retrieved for writing to the init file. The XML writer is independent on Qt and there is code included which should correctly translate the XML characters.

    I'll test that on other Qt versions as well.

    Best regards,

    Matthias

  • JayJay
    edited November -1
    Matthias,

    You are right.

    I tried that again and couldn't duplicate the problem. There were a number of other things I did in those sessions, so problem might have originated from something else.

    Right now, it seems like, it's all smooth.

    Regards,
    ~J
Sign In or Register to comment.