Logitech G502 With WebStorm
Logitech G502 Hero is a high-performance gaming mouse with 11 programmable buttons. Logitech G HUB software is used to program these buttons as per our needs. If you’re a developer, you can use this mouse to improve your productivity. Today, I am going to tell you how to program the Logitech G502 mouse for JetBrains WebStorm IDE, using the Lua programming language. This method is a little bit advanced.
Prepare The IDE
First, you need to create shortcuts in WebStorm. Here, I created some shortcuts for some tasks. You can create your own shortcuts.
Ctrl + Shift + F — Global Search
Alt + Shift + F — Format Code
Ctrl + / — Comment Line
Ctrl + D — Duplicate Line
Alt + Shift + B — Create New Branch
Alt + Left Arrow — Go Back
Alt + Right Arrow — Go Forward
Adding Webstorm To GHub
After creating shortcuts, We need to create Macros In GHub software. Click the button on the top left corner and select “Games & Applications”. Then click on “ADD GAME OR APPLICATION”
Add Webstorm IDE as a program. Now in the added programs list, click on Webstorm IDE to activate it.
Creating Macros
While it is activated, in the bottom menu select “MACROS”. Now we have to create macros for the above Webstorm shortcuts in GHub. Click “ADD MACRO FOR THE SELECTED APP”. Give a name for your macro. Select NO REPEAT -> START NOW -> RECORD KEYSTROKES. Now press your shortcuts keys after pressing, click on STOP RECORDING -> Save. Create macros for all your shortcuts like this. If you have any problem please watch this video.
Lua Scripting
Now let’s dig into the scripting part. In the bottom menu, select PROFILE and click on scripting
It will open a window like this. Here we write our Lua script to program our G502
We write our code inside this “OnEvent(event, arg)” function. First, create a table including mouse button values, and create two mouse events. Don’t change these values.
Buttons = {
Foward = 5,
Bacward = 4,
Up = 8,
Down = 7,
LeftScroll = 11,
RightScroll = 10,
G = 6,
Profile = 9,
Mid = 3
}
MOUSE_CLICK = "MOUSE_BUTTON_PRESSED"
MOUSE_RELEASED = "MOUSE_BUTTON_RELEASED"
We can assign more than one macro to a single mouse button by checking the delay to press and release the mouse button. I will assign two macros for a single mouse button. For that let’s define two delays. These delays are in milliseconds
SHORT_DELAY = 350
LONG_DELAY = 1000
Next, in an if-else block, we check which button is pressed, and which mouse event is triggered, and execute the relevant macro according to the delay. Inside the PlayMacro()
function place your macro name
if arg == Buttons.G then
if event == MOUSE_CLICK then
PressedTime = GetRunningTime()
ClickDuration = nil
elseif event == MOUSE_RELEASED then
ClickDuration = GetRunningTime() - PressedTime
-- Find All
if ClickDuration < SHORT_DELAY then
PlayMacro("Find")
-- Take SS
else
PlayMacro("Screenshot")
end
end
return
end
Like the above code, we can write code for all buttons. The final code should be like this. You can extend this code for your macros as you need. This is only a sample code.
function OnEvent(event, arg)
debug = false
-- Disable All These Buttons In GHub
Buttons = {
Foward = 5,
Bacward = 4,
Up = 8,
Down = 7,
LeftScroll = 11,
RightScroll = 10,
G = 6,
Profile = 9,
Mid = 3
}
SHORT_DELAY = 350
LONG_DELAY = 1000
DOUBLE_CLICK_DELAY = 200
MOUSE_CLICK = "MOUSE_BUTTON_PRESSED"
MOUSE_RELEASED = "MOUSE_BUTTON_RELEASED"
if arg == Buttons.G then
if event == MOUSE_CLICK then
PressedTime = GetRunningTime()
ClickDuration = nil
elseif event == MOUSE_RELEASED then
ClickDuration = GetRunningTime() - PressedTime
-- Macro for short click
if ClickDuration < SHORT_DELAY then
PlayMacro("YOUR_MACRO_NAME_HERE")
-- Macro for long click
else
PlayMacro("YOUR_MACRO_NAME_HERE")
end
end
return
elseif arg == Buttons.Down then
if event == MOUSE_CLICK then
PressedTime = GetRunningTime()
ClickDuration = nil
elseif event == MOUSE_RELEASED then
ClickDuration = GetRunningTime() - PressedTime
-- Macro for short click
if ClickDuration < SHORT_DELAY then
PlayMacro("YOUR_MACRO_NAME_HERE")
-- Macro for long click
else
PlayMacro("YOUR_MACRO_NAME_HERE")
end
end
return
end
end
Save your Lua script by pressing Ctrl + S. To activate these macros, you need to disable the mouse buttons, that you used in the above code. Unless these macros will not work.
Now we are done. When you are using Webstorm, you can do your work more quickly. I hope this will help you to improve your coding speed.
References
- https://douile.com/logitech-toggle-keys/APIDocs.pdf — Logitech G-Series Lua API Douile
See you from another article. Happy Coding.
You can connect with me on https://hirushafernando.com/
Subscribe to my newsletter
Read articles from Hirusha Fernando directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Hirusha Fernando
Hirusha Fernando
I am a web application developer with 3+ years of work experience. Currently I am following BSc. (Hons) in Artificial Intelligence degree at University Of Moratuwa. I love to work with new technologies. I love to face challenges. I am interested in tech stuff