0. Introduction

Two days ago I saw this youtube video by Linus Tech Tips and I decided I should develop something similar in order to create schematics and pcbs faster with Eagle in Ubuntu 14.04. The script I developed is based on Python2.7, using evdev and keyboard libraries and some basic ones, like subprocess and time.

1. Libraries installation

An easy way to install all these libraries is using the Pip tool. You can install it typing in the terminal:

sudo apt-get install python-pip

Next you can install all the libs:

sudo pip install evdev
sudo pip install keyboard

2. Finding all necessary parameter

Now you have to find your keyboard. In fact, in this example you’ll use an external keyboard which will be used only for macros. Just type before connecting your keyboard:

lsusb

It will print something like this:

Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 004: ID 0bda:57de Realtek Semiconductor Corp. 
Bus 001 Device 003: ID 0bda:0129 Realtek Semiconductor Corp. RTS5129 Card Reader Controller
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

Now connect the USB keyboard and redo the command:

lsusb

And find the name of the device. In my case:

Bus 001 Device 002: ID 1d6b:0002 SIGMA USB keyboard

Than, you have to find the /dev/input/ file. So:

ls /dev/input/by-id/

This will have an output like:

usb-04081-0005530015351010457_USB2.0_VGA_UVC_WebCam_200901010001-event-if00
usb-SIGMACHIP_USB_Keyboard-event-kbd

With all devices you have connected. So the file you need is:

/dev/input/by-id/usb-SIGMACHIP_USB_Keyboard-event-kbd

The last data you need is the xinput number. So type:

xinput -list

and see the id number of your keyboard (in my case is 18).

3. Setting the shortcut keys in Eagle

You have now to set your shortcut in Eagle in order to connect the keyboard to the functions. I mapped it in this way:

  • Num Lock → Ctrl + Shift + Alt + A → Group

  • “/” → Ctrl + Shift + Alt + B → Move

  • “*” → Ctrl + Shift + Alt + C → DRC

  • “-” → Ctrl + Shift + Alt + D → Copy

  • 7 → Ctrl + Shift + Alt + E → Paste

  • 8 → Ctrl + Shift + Alt + F → Delete

  • 9 → Ctrl + Shift + Alt + G → Add

  • 4 → Ctrl + Shift + Alt + H → Name

  • 5 → Ctrl + Shift + Alt + I → Value

  • 6 → Ctrl + Shift + Alt + J → Line

  • 1 → Ctrl + Shift + Alt + K → Text

  • 2 → Ctrl + Shift + Alt + I → Vias

  • 3 → Ctrl + Shift + Alt + L → Autorouter

  • 0 → Ctrl + Shift + Alt + M → Ratsnest

  • “.” → Ctrl + Shift + Alt + N → Hide Ratsnest

  • “+” → Ctrl + Shift + Alt + O → Route

  • Enter → Ctrl + Shift + Alt + P → Ripup

To add these shortcuts to Eagle, go to OptionsAssign and click on the New button. Select Ctrl, Alt, and Shift. In the Assigned command field, enter the code above, (apart from “Hide Ratsnest”, for which you have to insert: RATSNEST; RIPUP @; )

4. Running the code

This is the script:

#macro.py
from evdev import InputDevice, categorize, ecodes
import keyboard
import time
import subprocess

device = InputDevice("/dev/input/by-id/usb-SIGMACHIP_USB_Keyboard-event-kbd") # my keyboard (put here the file you find before)
for event in device.read_loop():
    if event.type == ecodes.EV_KEY:
        print(categorize(event));
        str1 = str(categorize(event));
        str1 = str1.split('(', 1 );
        str1 = str1[1].split(')', 1 );
        input =  str1[0];
        subprocess.call(["xinput", "set-prop", "18", "Device Enabled",  "0"]) # change "18" with your id number
            if(input == "KEY_NUMLOCK"):
                keyboard.press_and_release('ctrl+shift+alt+a')
        if(input == "KEY_KPSLASH"):
        keyboard.press_and_release('ctrl+shift+alt+b')
            if(input == "KEY_KPASTERISK"):
                    keyboard.press_and_release('ctrl+shift+alt+c')
                    time.sleep(.1)
                    keyboard.press_and_release('enter')
            if(input == "KEY_KPMINUS"):
                    keyboard.press_and_release('ctrl+shift+alt+d')
            if(input == "KEY_KP7"):
                    keyboard.press_and_release('ctrl+shift+alt+e')
            if(input == "KEY_KP8"):
                    keyboard.press_and_release('ctrl+shift+alt+f')
            if(input == "KEY_KP9"):
                    keyboard.press_and_release('ctrl+shift+alt+g')
            if(input == "KEY_KP4"):
                    keyboard.press_and_release('ctrl+shift+alt+h')
            if(input == "KEY_KP5"):
                    keyboard.press_and_release('ctrl+shift+alt+i')
            if(input == "KEY_KP6"):
                     xinput -listkeyboard.press_and_release('ctrl+shift+alt+j')    
            if(input == "KEY_KP1"):
                    keyboard.press_and_release('ctrl+shift+alt+k')
            if(input == "KEY_KP2"):
                    keyboard.press_and_release('ctrl+shift+alt+l')
            if(input == "KEY_KP3"):
                    keyboard.press_and_release('ctrl+shift+alt+m')
            if(input == "KEY_KP0"):
                    keyboard.press_and_release('ctrl+shift+alt+n')
            if(input == "KEY_KPDOT"):
                    keyboard.press_and_release('ctrl+shift+alt+o')
            if(input == "KEY_KPPLUS"):
                    keyboard.press_and_release('ctrl+shift+alt+p')
            if(input == "KEY_KPENTER"):
                    keyboard.press_and_release('ctrl+shift+alt+q')   

To run it, just type:

sudo python macro.py

That’s it!

You should be able to change between the Eagle functions using the numeric keypad of the keyboard.

Here I show how I use it:

Edit: I found a bug which can cause some troubles: if you type:

sudo python macro.py

Sometimes it doesn’t ask the passwd and for this reason it doesn’t start. I found a solution until I find the problem: before executing the script, just update the repo so that you have to insert the sudo passwd and then it works just fine:

sudo apt-get update
sudo python macro.py