Send keystrokes to Zwift with AutoHotkey

There is a trick to make AutoHotkey send keystrokes to Zwift.

The Administrator trick

It is important that the script is run as administrator or with UI access (an option in the AutoHotkey Setup) because that is the only way it can send keystrokes to Zwift.

This script shows how to do this. You can use it in your own AutoHotkey scripts.

In this script I handle that by letting the script check at start if it is run as administrator, and if not it will restart itself as such.

Use ControlSend

Another useful trick is to use ControlSend instead of Send.

There are two Zwift specific functions defined in the script:

ZwiftSendKey: Sends a text as keystrokes to Zwift. It is useful if you want to do things like open the workout menu or control direction at intersections.

ZwiftSendMessage: Sends a text as a chat message.

Both functions use ControlSend(Raw) so they work even if Zwift is not the currently active window.

 

Download

This script works for certain on Windows 10 with the latest versions of AutoHotkey and Zwift.

Download “zwift-send-examples.ahk”

zwift-send-examples.ahk – Downloaded 847 times – 2.25 KB

 

Code

This is the script itself.

 

/*

$Date: 2018/01/14 16:58:24 $
Version 1

Author:
Jesper Rosenlund Nielsen
http://zwifthacks.com

Script:
zwift-send-examples

Functionality:
Send keystrokes to Zwift
Send chat messages in Zwift

Usage:
Script must be run as Administrator to work. If not it will try to launch itself in Administrator mode.

(An alternative solution would be to add the option 'Run with UI access' to context menus (via the AutoHotkey Setup) and use that option to launch the script)


License:
CC NY-NC
https://creativecommons.org/licenses/by-nc/4.0/

*/

; Directives
; ==========
#SingleInstance Force
#NoEnv

; Configuration
; ==============

KeyDelayFactor := 3 ; increase if some keypresses do not transmit properly
ZwiftWindow := "ahk_class GLFW30" ; The ZwiftApp window

; MAIN ROUTINE
; ===============

; Script must be run as Administrator to work
ElevateScript()

; Initialisation
SetKeyDelay, % KeyDelayFactor*10, % KeyDelayFactor*10

; Example 1:
; Send key E to open workout window
MsgBox, Send key E to open workout window
ZwiftSendKey("e")

; Example 2:
; Send key M to open chat box, enter text, and press Enter to send message
MsgBox, Send key M to open chat box`, enter text`, and press Enter to send message
ZwiftSendMessage("Hi Gerrie!")

ExitApp

; END OF MAIN ROUTINE


; ---------------
; FUNCTIONS

; Send Zwift chat message
ZwiftSendMessage(message) {
 global ZwiftWindow
 ; Only write the message if Zwift is open
 if WinExist(ZwiftWindow){
 ; Insert message
 ControlSend, ahk_parent, m, % ZwiftWindow
 ControlSendRaw, ahk_parent, % message, % ZwiftWindow
 ControlSend, ahk_parent, {ENTER}, % ZwiftWindow
 } 
}

; Send Zwift keystroke(s)
ZwiftSendKey(message) {
 global ZwiftWindow
 ; Only send the key if Zwift is open
 if WinExist(ZwiftWindow){
 ControlSend, ahk_parent, % message, % ZwiftWindow
 }
}

; Restart this script in Administrator mode if not started as Administrator
ElevateScript() {
 full_command_line := DllCall("GetCommandLine", "str")

if not (A_IsAdmin or RegExMatch(full_command_line, " /restart(?!\S)"))
 {
 try
 {
 if A_IsCompiled
 Run *RunAs "%A_ScriptFullPath%" /restart
 else
 Run *RunAs "%A_AhkPath%" /restart "%A_ScriptFullPath%"
 }
 ExitApp
 }
}

 

 

9 comments

  1. Hi Jesper. Thanks for this, I’ve used it to get my mini keypad to send space for power-up, 6 and 1 for view angles regardless of which program has the focus in Windows. Fantastic! However, I can’t get it to press the left, down or right arrows to change or choose direction – I tried ZwiftSendKey(“Down”) and ZwiftSendKey(Down). Can you help?
    Thanks
    Nick

  2. Hi Jesper, many thanks for your solution! I’ve been looking for a way to use Zwift hotkeys without having to focus on Zwift itself for a long time.

    One question: This script works well and reliably for pressing a key once. Can it also be adapted so that you can use it to control the camera in the drone view? The decisive factor here is how long the key is held down. Unfortunately, I failed terribly trying to use the endless possibilities of AutoHotkey. Thanks in advance!

    1. It should be possible with something like this:

      ZwiftSendKey(“0”) ; activate drone view
      ZwiftSendKey(“{Left down}”) ; to press (and hold) the Left key
      Sleep, 4000 ; sleep for 4 seconds
      ZwiftSendKey(“{Left up}”) ; release the Left key

      1. So the camera is panned exactly for the predefined time, in this case 4 seconds and I get dizzy 🙂

        It would be nice if Autohotkey recognizes how long the key is pressed in order to pan the camera for exactly that long.

        1. If I understand the matter to some extent, I have to be able to identify the hotkey for it. In my case this is an Elgato Stream Deck and I have no idea whether or how it can be addressed. (AutoHotStreamDeck could be a solution, but I’ll have to study computer science first.)

          With a KeyDelayFactor between 20 and 30, the camera can be controlled to some extent even without a “return channel” by pressing several times.

  3. This is great. I love messing around with this stuff. I am however, having a really hard time even just figuring out how to click on the settings button. I Want to be able to open the settings menu and preferably be able to adjust the Trainer Intensity on the fly (I ride a fixed gear, so it’s basically my gears). Even just a quick shortcut to the settings would be a good start, but following the code in the zwift_hotkeys does seem to trigger anything.

    I tried enabling debugging, but where does the debug info show?

    Thanks,
    -Benson

    1. The AutoHotkey command OutputDebug is described here: https://www.autohotkey.com/docs/commands/OutputDebug.htm. You can use DebugView from Microsoft (linked at that page) to see the debug output. Alternatively you can use some other of the described debugging tools also linked there. Personally, I normally edit and debug with Visual Studio Code from Microsoft.

      To access settings you have to simulate mouseclicks in the right places. The functions in zwift-hotkeys you are looking for are ZwiftClick / ZwiftClickCenter / ZwiftClickRight / ZwiftClickLeft. The different functions are there because there are differences in how screen elements move and scale (or not) when the Zwift window scales. ZwiftClickCenter are for elements which stay anchored to the center of the screen and only scale up to a certain point; *Right and *Left do similarly. It is easy to see which function is the right one to use if you put Zwift in window mode and resize the window so its is ultrawide (e.g. full screen width but only a quarter screen height).

      There is already a click triggered by Backspace which will take you to the paused/menu screen so what you have to figure out is how to click in the spot which will open the Settings screen.

Leave a Reply to Nick K Cancel reply

Your email address will not be published. Required fields are marked *