Chat with preconfigured messages

(Editor’s note: This is an excellent example of how to use AutoHotkey to automate things while zwifting)

With the help from Jesper’s script, I created a hotkey script that type pre-configured messages in Zwift. I thought this will be handy when leading group rides since there are a basic pattern of messages that I use.

You will need to edit two txt files (Data.txt and Motivation.txt) with the messages (one per line).

Data is the usual ride stuff and is controlled by the right CTRL key. Motivation is for motivational words (This will be typed in random order) and is controlled by the right ALT key.

All three files (the script itself and the two txt files) need to be in the same directory. You have to have Autohotkey installed on your PC.

Download

Download “zwift-send-from-file.ahk”

zwift-send-from-file.ahk – Downloaded 623 times – 2.47 KB

 

Code

Here is what the script looks like:

/*

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

Authors:
Gerrie Delport

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

; Press Right CTRL to type messages line by line from Data.txt file:
; Send key M to open chat box, enter text, and press Enter to send message
MsgBox, Use Right Ctrl key for Data.txt and Right Alt key for Motivation.txt`,
i=0
RCtrl::
FileReadLine, line, %A_WorkingDir%\Data.txt, ++i
if !ErrorLevel
 ;Send % line
 ZwiftSendMessage(line)
else
 
 MsgBox, End of file
 
return

RAlt::
FileRead, line1, %A_WorkingDir%\Motivation.txt

Sort, line1, Random

RegExMatch(line1, ".*", line1)

ZwiftSendMessage(line1)


; 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
 }
}

One comment

  1. Thanks for adding this. This is a huge help wile I lead the Zwift rides.

Leave a Reply to Gerrie Delport Cancel reply

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