Update LedKeyboardShortcuts.ahk

Uses a gui for the notifications
This commit is contained in:
flagstone78 2018-04-17 02:48:43 -06:00 committed by GitHub
parent fc4ce0489f
commit 69f76acb6d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -2,9 +2,19 @@
#SingleInstance force ; only lets one run
SetBatchLines, -1
#Persistent ; prevents the script from exiting automatically
socket := new Example("ws://192.168.1.33:81") ; !!! Update this !!!
connected := 0 ; state variable for server connection
; Register a function to be called on exit:
OnExit("ExitFunc")
; make the notification box
notificationEnable := true ; set this to false to disable the notification popups
notificationTime := 1 ; seconds the notifications stay
n := new Notification("Starting led shortcuts!", 200,14, ,notificationTime)
; make the socket
socket := new McLightingServer("ws://192.168.1.33:81") ; replace with the ip address of the mclighting controller
incrementAmount := 10 ; how finely you adjust the color, speed, and brightness per keypress
maxColor := 255
@ -18,13 +28,12 @@ brightness := 127
speed := 127
mode := 1
^#Numpad4:: ; switch modes down
mode := mode - 1
if(mode < 0)
mode := maxMode
cmd = /%mode% ; / is the set effect mode command
TrayTip, , Mode: %mode%
setNotification("Mode: " + mode)
sendCmd(cmd)
; MsgBox %cmd%
return
@ -34,19 +43,19 @@ mode := 1
mode := mode + 1
if(mode > maxMode)
mode := 0
TrayTip, , Mode: %mode%
setNotification("Mode: " + mode)
cmd = /%mode%
sendCmd(cmd)
; MsgBox %cmd%
return
^#Numpad5:: ; turn off
TrayTip, , Leds are off
setNotification("Leds are off")
sendCmd("=off") ; = is the set control command
return
^#Numpad0:: ; turn on to static color
TrayTip, , Leds are on
setNotification("Leds are on")
cmd := "*"+toHexColor(red,green,blue) ; * is the set all command
sendCmd(cmd) ; = is the set control command
return
@ -57,7 +66,7 @@ mode := 1
if(brightness < 0)
brightness := 0
cmd = `%%brightness% ; % is the set brightness mode command. ` is the escape character
TrayTip, , Brightness: %brightness%
setNotification("Brightness: " + brightness)
sendCmd(cmd)
; MsgBox %cmd%
return
@ -67,7 +76,7 @@ mode := 1
if(brightness > maxColor)
brightness := maxColor
cmd = `%%brightness% ; % is the set brightness mode command
TrayTip, , Brightness: %brightness%
setNotification("Brightness: " + brightness)
sendCmd(cmd)
; MsgBox %cmd%
return
@ -77,7 +86,7 @@ mode := 1
if(speed < 0)
speed := 0
cmd = ?%speed% ; ? is the set brightness mode command
TrayTip, , Speed: %speed%
setNotification("Speed: "+ speed)
sendCmd(cmd)
; MsgBox %cmd%
return
@ -87,7 +96,7 @@ mode := 1
if(speed > maxColor)
speed := maxColor
cmd = ?%speed% ; ? is the set brightness mode command
TrayTip, , Speed: %speed%
setNotification("Speed: "+ speed)
sendCmd(cmd)
; MsgBox %cmd%
return
@ -100,7 +109,7 @@ mode := 1
cmd := "#"+toHexColor(red,green,blue) ; # is the set main color command
; MsgBox %cmd%
TrayTip, , Red: %red%
setNotification("Red: " + red)
sendCmd(cmd)
return
@ -112,7 +121,7 @@ mode := 1
cmd := "#"+toHexColor(red,green,blue) ; # is the set main color command
; MsgBox %cmd%
TrayTip, , Red: %red%
setNotification("Red: " + red)
sendCmd(cmd)
return
@ -124,7 +133,7 @@ mode := 1
cmd := "#"+toHexColor(red,green,blue) ; # is the set main color command
; MsgBox %cmd%
TrayTip, , Green: %green%
setNotification("Green: " + green)
sendCmd(cmd)
return
@ -136,7 +145,7 @@ mode := 1
cmd := "#"+toHexColor(red,green,blue) ; # is the set main color command
; MsgBox %cmd%
TrayTip, , Green: %green%
setNotification("Green: " + green)
sendCmd(cmd)
return
@ -148,7 +157,7 @@ mode := 1
cmd := "#"+toHexColor(red,green,blue) ; # is the set main color command
; MsgBox %cmd%
TrayTip, , Blue: %blue%
setNotification("Blue: " + blue)
sendCmd(cmd)
return
@ -160,23 +169,24 @@ mode := 1
cmd := "#"+toHexColor(red,green,blue) ; # is the set main color command
; MsgBox %cmd%
TrayTip, , Blue: %blue%
setNotification("Blue: " + blue)
sendCmd(cmd)
return
sendCmd(cmd)
{
global connected
global socket
; MsgBox %connected%
if( connected )
; MsgBox connected: %connected%
if( socket.connected )
{
socket.Send(cmd)
; MsgBox %cmd%
}
else
TrayTip, , Not connected to server!
setNotification("Not connected to server!")
return
}
toHexColor(r, g, b)
@ -191,14 +201,38 @@ toHexColor(r, g, b)
return ""+color ;
}
class Example extends WebSocket
setNotification(message)
{
global notificationEnable
global n ; the notification class
if(notificationEnable)
{
n.updateMessage(message)
n.nshow()
}
return
}
ExitFunc(ExitReason, ExitCode)
{
; MsgBox Exiting
global n ; notification class
n.ndestroy() ; deletes the notification
return 0 ; must return zero to exit
}
class McLightingServer extends WebSocket
{
OnOpen(Event)
{
TrayTip, ,Connection established to led controller!
setNotification("Connection established!")
; InputBox, Data, WebSocket, Enter some text to send through the websocket.
; this.Send(Data)
global connected := 1
; global connected
this.connected := 1
; MsgBox connection %connected%
}
OnMessage(Event)
@ -209,22 +243,24 @@ class Example extends WebSocket
OnClose(Event)
{
TrayTip, , Websocket Closed
global connected := 0
setNotification("Websocket Closed!")
; MsgBox closed!
this.connected := 0
this.Disconnect()
ExitApp
}
OnError(Event)
{
TrayTip, , Websocket Error
global connected := 0
setNotification("Websocket Error")
; MsgBox error!
this.connected := 0
this.Close()
}
__Delete()
{
TrayTip, , Exiting
; setNotification(" Exiting Led controller ") ; sticks after program closes
ExitApp
}
}
@ -233,6 +269,7 @@ class WebSocket
{
__New(WS_URL)
{
this.connected := false ; flag for connection status
static wb
; Create an IE instance
@ -295,3 +332,87 @@ class WebSocket
}
}
}
class Notification
{
__New(message, pnW=700, pnH=300, position="b r", time=10000)
{
this.showTime := time ; time that the notification shows up for
global pn_msg
this.pn_msg := message ; message to display
Gui, Notify: +AlwaysOnTop +ToolWindow -SysMenu -Caption +LastFound
this.PN_hwnd := WinExist()
WinSet, ExStyle, +0x20
WinSet, Transparent, 0 ; makes the box transparent so that it can fade in and out. 255 -> opaque; 0 -> transparent
Gui, Notify: Color, 0x111111 ; background color
Gui, Notify: Font, cWhite s10 w500, Terminal ; message color, size, weight, and font
Gui, Notify: Add, Text, % " x" 20 " y" 12 " vpn_msg", % this.pn_msg ; add message
RealW := pnW + 50
RealH := pnH + 20
Gui, Notify: Show, W%RealW% H%RealH% NoActivate
this.WinMove(this.PN_hwnd, position)
global windowID := ("ahk_id "this.PN_hwnd) ; window id
this.nshow()
}
ndestroy() {
this.winfade(windowID,0,50) ; fades the box out
Gui, Notify: Destroy
return
}
nhide(){ ; called by the timer
showTimer:
SetTimer, showTimer, Off ; turn off the timer
global windowID
winfade(windowID,0,50) ; fades the box out
return
}
nshow(){
w:= ("ahk_id "+this.PN_hwnd)
WinGet,s,Transparent,%w% ; makes the notification visible
s:=(s="")?255:s ;prevent trans unset bug
WinSet,Transparent,210,%w%
Closetick := this.showTime*1000
SetTimer, showTimer, % Closetick ; reset the hide timer
return
}
updateMessage(message) {
this.pn_msg := message
GuiControl, Notify: Text, pn_msg, % this.pn_msg
return
}
WinMove(hwnd,position) {
SysGet, Mon, MonitorWorkArea
WinGetPos,ix,iy,w,h, ahk_id %hwnd%
x := InStr(position,"l") ? MonLeft : InStr(position,"hc") ? (MonRight-w)/2 : InStr(position,"r") ? MonRight - w : ix
y := InStr(position,"t") ? MonTop : InStr(position,"vc") ? (MonBottom-h)/2 : InStr(position,"b") ? MonBottom - h : iy
WinMove, ahk_id %hwnd%,,x,y
return
}
}
; winfade must be global for the timer to use it
winfade(w:="",t:=128,i:=1,d:=10) {
w:=(w="")?("ahk_id " WinActive("A")):w
t:=(t>255)?255:(t<0)?0:t
WinGet,s,Transparent,%w%
s:=(s="")?255:s ;prevent trans unset bug
WinSet,Transparent,%s%,%w%
i:=(s<t)?abs(i):-1*abs(i)
while(k:=(i<0)?(s>t):(s<t)&&WinExist(w)) {
WinGet,s,Transparent,%w%
s+=i
WinSet,Transparent,%s%,%w%
sleep %d%
}
return
}