DEV Community

Cover image for My G815 Keyboard is on Steroids (Shortcut helper)
klyse
klyse

Posted on

My G815 Keyboard is on Steroids (Shortcut helper)

Logitech G815 Shortcut Helper

I wrote a small shortcut helper in GoLang that helps me to remember the most useful shortcuts for IntelliJ based products tested with Rider and GoLand and a QWERTZ keyboard.

Intro

I recently bought a Logitech G815 which turned out to be a great decision for multiple reasons. The obvious one being that it's a great keyboard ✨. The other one is that I found out Logitech provides an SDK for the keyboard => 👌.

RGB Keys

The keyboard supports RGB background lighting for each key. There are plenty of integrated functions such as a color wave which looks great🌟. The keyboard is made for gaming purposes and yes it does integrate with a lot of games but I didn't try any myself🙈.

SDK

The Logitech SDK can be found on this website: https://www.logitechg.com/en-us/innovation/developer-lab.html.

I was particularly interested in the LED Illumination SDK.

The SKD is written in C++ and provides the .lib, .h, .dll, and some examples and is windows only.

Integration with GO

I'm currently learning GoLang and I wanted to program this in go and not in C++ or C#.

Calling native C++ functions from GO

I found a great way to call native C++ functions from Go using the sys module.

Original repo: https://github.com/golang/go/wiki/WindowsDLLs

type LogiKeyboard struct {
    dll                             *windows.LazyDLL
    ledInit                         *windows.LazyProc
    // ...
}

func Create() *LogiKeyboard {
    p := new(LogiKeyboard)

    // initialization of the dll
    p.dll = windows.NewLazyDLL("LogitechLedEnginesWrapper.dll")

    // assign the function
    p.ledInit = p.dll.NewProc("LogiLedInit")
    //...
    return p
}

// wrapper for the init function
func (v LogiKeyboard) Init() {
    ret, _, _ := v.ledInit.Call()

    if ret != 1 {
        log.Fatal("Already initialized")
    }
}

Enter fullscreen mode Exit fullscreen mode

Additionally, I had to translate the .h file in go syntax. This helps to have all the constants (like defines) in one place and reduce code duplication. Here is the file.

Integration with Windows

I needed a slim and easy way to catch Windows keystrokes. Turns out there is a great module for that as well: github.com/moutend/go-hook.

main.go:

if err := run(shortcuts); err != nil {
    log.Fatal(err)
}

func run(shortcuts []Shortcuts.Shortcut) error {
    // Buffer size is depends on your need. The 100 is placeholder value.
    keyboardChan := make(chan types.KeyboardEvent, 100)
    if err := keyboard.Install(nil, keyboardChan); err != nil {
        return err
    }

    signalChan := make(chan os.Signal, 1)
    signal.Notify(signalChan, os.Interrupt)
    for {
        select {
        case <-time.After(5 * time.Minute):
            fmt.Println("Received timeout signal")
            return nil
        case <-signalChan:
            fmt.Println("Received shutdown signal")
            return nil
        case k := <-keyboardChan:
            // check if shortcut modifiers are pressed and set RGB color
        }
        continue
    }
}
Enter fullscreen mode Exit fullscreen mode

Those few lines of code are everything you need to create a keylogger in go. Isn't that crazy?

Shortcuts

Shortcuts can be modified in main.go:

var shortcuts = []Shortcuts.Shortcut{
    // if the left shift key is presed:
    // F6 will start blinking red
    // F9 will change the color to blue
    Shortcuts.CreateWithKey([]types.VKCode{types.VK_LSHIFT}, []Shortcuts.ShortcutKey{
        Shortcuts.CreateKeyColorEffect(LogiKeyboardTypes.F6, 100, 0, 0, Shortcuts.Blinking),
        Shortcuts.CreateKeyColor(LogiKeyboardTypes.F9, 0, 0, 100),
    }),
    //...
    }
Enter fullscreen mode Exit fullscreen mode

Integration with Logitech GHub

The keyboard has five G keys and each key can be assigned with a different task. One task is to launch an application => great :). The downside of this function is that an application can be started but not stopped. I wanted to start and stop the shortcut helper with the same key just like a toggle.

As always there is a workaround in PowerShell😂:

Set-Location -Path C:\Projects\LogitechKeyboardLED

$ProcessActive = (Get-Process logitechKeyboardLed -ErrorAction SilentlyContinue).Id
if($null -eq $ProcessActive)
{
    Start-Process ./logitechKeyboardLed.exe -NoNewWindow
}
else
{
    Stop-Process $ProcessActive
}

Enter fullscreen mode Exit fullscreen mode

Configure the G key using the Launch Application option:

Shortcuthelper in action

Github Repo

GitHub logo klyse / LogitechKeyboardLED

Logitech G815 / G915 LED control written in GO

Logitech G815 ⌨️ Rider and GoLand Shortcut helper

This is a little shortcut helper written in GO 👨‍💻. The software can be started using one of the G keys. Once started the process profiles every keystroke made on the keyboard. If a certain combination ex. SHIFT+CTRL is pressed the most important shortcuts for Rider / GoLang are visualized on the keyboard using the RGB key background lightning 💡.

Here's a quick example (sorry for the bad quality, the keys are shining bright and clear in reality🙈):

Here's another one:

How to install 💻

  1. Clone the repo
  2. Build the source code go build -o LogitechKeyboardLED.exe ./ (win only)
  3. Navigate to the GHub
  4. Add a new launch command with the following params:

The script checks the state of the process and starts it if the process is stopped, and stops it if the process is running. This way…

Thanks for reading :)

Top comments (6)

Collapse
 
ashoutinthevoid profile image
Full Name • Edited

There are an abundance of custom and premade keyboards that run the completely open source QMK firmware. At least from what I gathered from this article, you could do everything you did here (and all of the other QMK features) in an OS agnostic manner. There's no reason to log keys in the OS in order to drive the hardware (leds); you can do that directly on the hardware itself.

Collapse
 
klyse profile image
klyse

I wasn't aware of the QMK firmware. But if this works with the Logitech G815 keyboard this could really be a great alternative. Thanks for the comment :)

Collapse
 
pavel_polivka profile image
Pavel Polívka

This is amazing. I do not like the windows only part tho...

Collapse
 
klyse profile image
klyse

Thanks :) Yes I know, I don't like windows only either... The Logitech SDK is windows only.

Collapse
 
pavel_polivka profile image
Pavel Polívka

Now I want to buy keyboard with API that would work on Mac 😂

Collapse
 
aayushg159 profile image
Aayush Gupta

This post motivated me to create something similar for my asus rog laptop which has each key RGB 😀