Reply
Logi Browser
oldtaku
Posts: 11
Registered: ‎01-31-2009

G13/G15 Advanced Scripting Techniques How-to

[ Edited ]

UPDATE: This is now outdated with the G-13 drivers as of Dec 18 2009. The basic concepts still apply, but it's no longer necessary since the drivers are advanced enough that you don't need this specific example.

 

 

 

First, this is techie rambling and spammy and involves writing scripts, so if you just want to play your games, read no further.

 

This is what I do to make my G15 and G13 indispensible as productivity tools. The Profiler is pretty decent as far as letting you assign simple keystrokes and launch actions to a key, but unless anyone objects horribly, I'm going to show you how to:

  1. Make a G-key launch an app if it isn't running yet. If it is running, toggle between either minimized/visible or moved to primary/secondary monitor.
  2. Make G-keys send mouse clicks.
  3. Make G-keys do things in other apps.
  4. Use your G13 joystick as mouse movements.
  5. Use your G13 joystick to adjust the view in World of Warcraft if you don't want to use it as movement.
  6. Use your G13 joystick to emulate mouse-wheel scroll.

Not going to say anything about writing LCD apps, because that's another huge topic, just the scripting here.

 

When Not to Use This

Use the profiler to  do the normal stuff - single keys or simple macros. No use re-inventing the wheel.

 

Disclaimer

I'm not with Logitech, just a user. They don't endorse or support any of this.

 

Installing AutoHotkey

Get AutoHotkey for free at http://www.autohotkey.com/ - install it, run it; you should see a green 'H' icon in your tray. Now create a myscripts.ahk file somewhere (in 'My Documents' is fine). All this scripting stuff is going to go in myscripts.ahk, and whenever you change it, right click the H icon and 'Open...' your myscripts.ahk (the first time) or after just 'Reload This Script'.

 

 Assigning a Unique Key to a G-key

 Now assign one of your G-keys (like G1) to some unique key combo you would never use by left clicking it in the profiler, choosing  'Assign Keystroke...' and hitting something like Ctrl-Alt-Win-1. The G15/13 will now send this keystroke whenever you hit it. Assign a different one to every key you want to program specially.

 

In AutoHotkey this particular sequence is matched by ^!#1  - the ^ is Ctrl, the ! is Alt, then # is Win, and the 1 is 1. Most special keys are recognized, so ^!#Left is Ctrl-Alt-Win-Leftarrow and ^!#F1 is Ctrl-Alt-Win-F1, etc.

 

Window Title Matching

One crucial thing we're worried about here is window title matching - how do you tell if a program is running?  By default AutoHotkey just matches the start of the Window title, which doesn't work at all for something like Firefox or Notepad++ which put their titles at the end. So the first thing you want at the top of myscript.ahk is:

 

; Match text anywhere in window title

SetTitleMatchMode 2

AutoHotkey has extensive help (right click the H tray icon, Help) if you want to explore the other options.

 

Window Spy

If you right-click the AHK tray icon, you'll see a 'Window Spy' option. This is extremely useful - it puts up a window that just monitors everything about the window your mouse is over. Particularly important are the Window Title at the top and the Active Window Position.

 

 Next: how to auto-launch apps and minimize/maximize move/restore run/close them.

 

 

 

 

Message Edited by oldtaku on 02-04-2009 12:51 AM
Message Edited by oldtaku on 02-04-2009 12:53 AM
Logi Browser
oldtaku
Posts: 11
Registered: ‎01-31-2009
0

Re: G13/G15 Advanced Scripting Techniques How-to

Let's assume you've assigned G1 to Ctrl-Alt-Win-1 as above.

 

Launch or Minimize/Maximize

 Now we want to make hitting it launch Firefox if it doesn't exist. If it does exist then we want to alternately minimize and restore the window. This looks like:

; Match anywhere in title
SetTitleMatchMode 2

; === Firefox - Ctrl-Alt-Win-1 ===
#IfWinExist, - Firefox
^!#1::MinMax(70,40)
#IfWinExist
#IfWinNotExist, - Firefox
^!#1::Run, "%programfiles%\Mozilla Firefox\firefox.exe"
#IfWinNotExist

; minimize/maximize
MinMax(x1,y1)
{
IfWinNotActive
{
WinActivate
WinMove %x1%,%y1%
} else {
WinMinimize
}
}

'#ifWinExist, - Firefox' says 'If there's a window with "- Firefox" in the title, do the following.' ^!#1::' means 'when Ctrl-Alt-Win-1 is hit do the following.'  'MinMax(70,40)' is a function we provide further down that minimizes the window if it's active, otherwise activates and moves it. Finally, the '#IfWinExist' with no title ends the #IfWinExist matching.

 

What if there is no Firefox running yet? '#IfWinNotExist, - Firefox' says 'If there's no window with "- Firefox" in the title do the following.' The '^!#1::' is Ctrl-Alt-Win-1 again, then 'Run, "%programfiles%\Mozilla Firefox\firefox.exe"' is Firefox's location. The  %programfiles% is a shortcut for 'C:\Program Files' or wherever windows thinks your programs directory is. I use quotes because the path contains a space. Then we close the '#IfWinNotExist' block.

 

The MinMax() function says if the window we just found (with #IfWinExist) is not active, then activate it and move it to this location. If it already was, then minimize it. You could optionally just delete the WinMove if you don't like that behavior. Once MinMax is in your .ahk file once, you can use it for as many keys as you like, which is why we made it a function.

 

Launch or Move Between Monitors

 I have a monitor to the left of my big monitor that I 'park' stuff on. For instance XYplorer (a file manager). I have my G2 key assigned to toggle moving XYplorer from the side monitor to my main monitor and vice versa. It looks like this, assuming you've assigned the G2 key to keystroke Ctrl-Alt-Win-2:

; Match anywhere in title

SetTitleMatchMode 2

 

# == XYplorer Ctrl-Alt-Win-2 ===
#IfWinExist, XYplorer
^!#2:: PriSec(270,370,-1129,368)
#IfWinExist
#IfWinNotExist, XYplorer
^!#2::Run, "%programfiles%\xyplorer\XYplorer.exe"
#IfWinNotExist

; switch between primary and secondary monitors
PriSec(x1,y1,x2,y2)
{
WinGetPos X, Y
if( X < 0 ) {
WinMove x1, y1
WinActivate
} else {
WinRestore
WinMove x2, y2
}
}

 Most of this should look just like the minimize/maximize example. The difference is that  we call PriSec instead.

 

PriSec takes the arguments x1,y1,x2,y2. x1,y1 are the upper left coordinates of the window on the primary screen, x2,y are the upper left coordinates of the window on the secondary screen. You can easily find these by running Window Spy (as in the first post in this thread) then moving the application window where you want it and looking at left (x) and top (y) in the Active Window Position in the Window Spy window.

 

Again, you only need PriSec (and the SetTitleMatchMode) once in your myscript.ahk file.

 

Launch/Close Program

 Finally, you may want to run a program if it doesn't exist and close it if it does. I have VNC (to remove control my linux server) mapped to Ctrl-Alt-Win-3:

; Match anywhere in title
SetTitleMatchMode 2

; == Cibo VNC Ctrl-Alt-Win-3 ===
#IfWinExist, root's X desktop
^!#3::WinClose, root's X desktop
#IfWinExist
#IfWinNotExist, root's X desktop
^!#3::Run "%programfiles%\UltraVNC\vncviewer.exe" localhost:1
#IfWinNotExist

 This should look pretty familiar. We don't even need a function for this, WinClose already takes care of it! Again, you only need the SetTitleMatchMode once at the top of your script.

 

Next: using the G13 as a mouse.

 

Logi Browser
oldtaku
Posts: 11
Registered: ‎01-31-2009
0

G13 Joystick as a Mouse

[ Edited ]

This one seems to come up a lot in the forums.

 

Sending Mouse Clicks

First, assigning a G key to emulate a mouse click is easy. Assign a G key (like the joystick center button, but any will work) to Ctrl-Alt-Win-c in the Logitech G-series Key Profiler. Now add this to your myscript.ahk:

 

#^!c::MouseClick,Left

Is that easy or what? You can use 'Middle' or 'Right' instead of 'Left' if you want to send those other buttons instead. If you look at the AutoHotKey help for MouseClick there are plenty of other options you can use with it, like only sending a down or up event or moving the pointer first, but this covers most of it.

 

Using the Thumbstick as a Mouse

This could be more compact, but it's readable and it works great!

 

In the G-Series Key Profiler, assign left on the thumbstick to Ctrl-Alt-Win-LeftArrow, right to Ctrl-Alt-Win-RightArrow, up to Ctrl-Alt-Win-UpArrow, and down to Ctrl-Alt-Win-DownArrow. Now add this to your myscript.ahk:

 

; These lines need to go at the top of the file before any key assignments or #Ifs

; MouseSpeed is the number of pixels to move per cycle

MouseSpeed=5
MouseStatus=0

 

; These can go anywhere

#^!Left::MouseSet(1)
#^!Left UP::MouseUnset(1)
#^!Right::MouseSet(2)
#^!Right UP::MouseUnset(2)
#^!Up::MouseSet(4)
#^!Up UP::MouseUnset(4)
#^!Down::MouseSet(8)
#^!Down UP::MouseUnset(8)
#^!c::MouseClick,Left

MouseSet( val )
{
global MouseStatus
if ( MouseStatus = 0 ) {
MouseStatus := val
MouseRepeat()
} else {
MouseStatus |= val
}
}
MouseUnset( val )
{
global MouseStatus
MouseStatus &= ~val
}

MouseRepeat()
{
global MouseStatus, MouseSpeed
Loop {
; Esc will get us out just in case
if ( MouseStatus = 0 ) or GetKeyState( "Esc", "D" )
break

dx = 0
dy = 0

if ( MouseStatus & 0x01 )
dx -= 1
if ( MouseStatus & 0x02 )
dx += 1
if ( MouseStatus & 0x04 )
dy -= 1
if ( MouseStatus & 0x08 )
dy += 1
dx := dx * MouseSpeed
dy := dy * MouseSpeed
MouseMove,%dx%,%dy%,1,R
}
}

The only tricky thing here is the '#^!Left UP' lines - the UP is an event that happens when a key is released (normally they trigger when a key is pressed down).  So the '#!^Left::' line starts moving left, then the '#^!Left UP::' line tells the running script that the left 'key' has been released. Each direction gets its own value in a bitfield - In this way we get diagonal movement.

 

Also, just in case the script runs away and your cursor won't stop (though I've never seen this happen), hitting Esc will abort the movement.

 

Thumbstick as WoW View Adjust

In case you want to use G-keys for movement in World of Warcraft and use the thumbstick for view adjustment, take the previous script and change the MouseRepeat() function (you still need all the other bits) to:

MouseRepeat()
{
global MouseStatus, MouseSpeed
wow := WinActive( "World of Warcraft" )
if ( wow != 0 )
MouseClick,Right,,,,,D,
Loop {
; Esc will get us out just in case
if ( MouseStatus = 0 ) or GetKeyState( "Esc", "D" ) {
if( wow != 0 )
MouseClick,Right,,,,,U,
break
}

dx = 0
dy = 0

if ( MouseStatus & 0x01 )
dx -= 1
if ( MouseStatus & 0x02 )
dx += 1
if ( MouseStatus & 0x04 )
dy -= 1
if ( MouseStatus & 0x08 )
dy += 1
if( wow = 0 ) {
dx := dx * MouseSpeed
dy := dy * MouseSpeed
} else {
dx := dx * 20
dy := dy * 20
}
MouseMove,%dx%,%dy%,1,R
}
}

This checks to see if WoW is your active window. If it is then using the thumbstick still does a mouse move, but when you start moving it it sends a right mouse click down (and holds it). When you're done moving, it sends a right mouse click up.

 

Furthermore, at the bottom of the script it moves the mouse by 20 instead of 5 (since I found 5 was too sluggish). You can adjust this to your liking (higher numbers = faster movement), and if you want to invert the axes you could make a 20 into a -20.

 

Now the nice thing is this version will still work exactly like the previous MouseRepeat() if WoW isn't your active window, so there's no harm in using it all the time.

Message Edited by oldtaku on 02-04-2009 02:30 AM
Logi Browser
oldtaku
Posts: 11
Registered: ‎01-31-2009
0

G13 Joystick as Mouse Wheel

Okay, last post for a bit. Here's how to use the G13 joystick as a mouse wheel. First, in the G-series Key Profiler assign the up and down for the joystick to something unique (like Ctrl-Alt-Win-u for up and Ctrl-Alt-Win-d for down), then add the following to your myscript.ahk:

; These need to go at the top of the file before any key defs or #If lines

WheelStatus=0
WheelDelay=50

 

; These can go anywhere

#^!u::WheelSet(1)
#^!u UP::WheelUnset(1)
#^!d::WheelSet(2)
#^!d UP::WheelUnset(2)

WheelSet( val )
{
global WheelStatus
if ( WheelStatus = 0 ) {
WheelStatus := val
WheelRepeat()
} else {
WheelStatus |= val
}
}
WheelUnset( val )
{
global WheelStatus
WheelStatus &= ~val
}

WheelRepeat()
{
global WheelStatus, WheelDelay
Loop {
; Esc will get us out just in case
if ( WheelStatus = 0 ) or GetKeyState( "Esc", "D" )
break
if ( WheelStatus & 1 )
Send {WheelUp}
if ( WheelStatus & 2 )
Send {WheelDown}
sleep, %WheelDelay%
}
}

 This should look familiar. It's just like joystick mouse movement stuff, but only for WheelUp and WheelDown. It's also far more complicated than it needs to be because there's only the two mutually exclusive conditions, but what the heck, code re-use.

 

 

Logi Browser
oldtaku
Posts: 11
Registered: ‎01-31-2009
0

Re: G13 Joystick as a Mouse

That's it, hope someone found this useful - if you have any questions or special function requests just ask. Read the AutoHelpKey help, you'll find it can do darn near anything including selecting app menu items, clicking boxes, moving the cursor relative, hopping to another app and back, pausing between steps, etc. Almost anything you might want to do that doesn't involve reading the content of a window you can assign to a G key. And don't forget you can use M1, M2, M3 to create different sets.

 

Moderator
CharlesB
Posts: 4,196
Registered: ‎07-06-2006
0

Re: G13/G15 Advanced Scripting Techniques How-to

[ Edited ]

Moved this individual response to a new thread.

 

(Looks like a brilliant how-to so far, and I didn't want to let it get polluted by unrelated questions)

Message Edited by CharlesB on 02-04-2009 11:13 AM

Logi Nu
Djerun
Posts: 1
Registered: ‎12-24-2009
0

Re: G13/G15 Advanced Scripting Techniques How-to

Hi. I got your script working for the joystick as a mouse.

 

But the curser moved with incredible speed. Withing a 1/10 second it completely crossed my monitor.

 

I decreased the mousespeed from 5 to 1 which reduced the curser speed from "uncontrollable" to "very hard controllable".

 

I cant set mouse speed below 1 (if I try 0,5 or 0.5 nothing happens when triggering the joystick)

 

Changing any mouse system options doesn't change anything thats because the script doesn't care about them I think.

 

How can I further decrease my Mousespeed?

Logi Apprentice
Davs
Posts: 126
Registered: ‎10-05-2009
0

Re: G13/G15 Advanced Scripting Techniques How-to

Oh, wow. Why would you use this outdated script after analog mouse became available to assign to the joystick?

Logi Guru
Trusselo
Posts: 1,323
Registered: ‎04-04-2009
0

Re: G13/G15 Advanced Scripting Techniques How-to

3.04 drivers just added last week added the feature of assigning to joystick. and the lua scripting found in the API scripting guied (under help in the scripting editor) has the script command for changing the mouse speed too. (last page before the included libraries)


My G13 Pofile Folder

Help me out. Get a Drop box account!
Logi Apprentice
Davs
Posts: 126
Registered: ‎10-05-2009
0

Re: G13/G15 Advanced Scripting Techniques How-to

More importantly though, the 3.04 drivers allow for very precise mouse movement since the speed is proportional to how far the stick is from center, something that was impossible to do using the stick in digital mode (up, down left, right nothing in between).