Skip to content

Instantly share code, notes, and snippets.

@Aatoz
Last active December 25, 2015 09:59
Show Gist options
  • Save Aatoz/6957872 to your computer and use it in GitHub Desktop.
Save Aatoz/6957872 to your computer and use it in GitHub Desktop.
CLeapModule.ahk
; License: public domain
; Feel free to modify and improve upon the work herein, but be certain to give credit me (Verdlin) when using my scripts
; Credits:
; Tidbit: st.ahk lib
; tkoi: ILButton.ahk
; If you see your work and you are not credit, this is not deliberate. Notify me and I will credit you ASAP
class CLeapModule
{
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;
__New(sGesturesIni, hMsgHandler, sExistingGUI="", iXOffset="", iYOffset="")
{
global
this.m_vGesturesIni := g_vGesturesIni := class_EasyIni(sGesturesIni)
; http://msdn.microsoft.com/en-us/library/windows/desktop/aa511453.aspx#sizing
static s_iMSDNStandardBtnW := 75, s_iMSDNStandardBtnH := 23, s_iMSDNStandardBtnSpacing := 6
g_hMsgHandler := hMsgHandler
this.m_vLeap := Leap(Func("LeapMsgHandler")) ; Note, this inits g_sLeapWorkingDir
if (!this.m_vLeap)
return ; An error message is already been display in a MsgBox, so just return blan
GUI, LeapModuleGUI_:New, hwndg_hLeapModuleGUI, Leap Settings
;~ -------------------------------------------------------------------------------------------------------
GUI, Font, s15 c83B8G7
GUI, Add, GroupBox, % "x18 y11 h" 224+(s_iMSDNStandardBtnSpacing*2)+s_iMSDNStandardBtnH " w750 Center", Leap Gestures Control Center
GUI, Font, s8
GUI, Add, Button, % "xp+596 yp+" 239+s_iMSDNStandardBtnH+s_iMSDNStandardBtnSpacing " w" s_iMSDNStandardBtnW " h" s_iMSDNStandardBtnH " gLeapModuleGUI_OKBtn", &OK
GUI, Add, Button, xp+80 yp wp hp gLeapModuleGUI_GUIClose, &Cancel ; This will bypass a Save, unless the Save button was clicked
;~ -------------------------------------------------------------------------------------------------------
;~ -------------------------------------------------------------------------------------------------------
local sLocGestures := g_vGesturesIni.GetSections("|")
GUI, Font, s12 c83B8G7
GUI, Add, GroupBox, x22 y40 h225 w237 Center, Select or add a gesture
GUI, Font, s8
GUI, Add, Text, % "xp+" s_iMSDNStandardBtnSpacing " yp+" s_iMSDNStandardBtnH+2 " h" s_iMSDNStandardBtnH, &Name:
GUI, Add, Edit, xp+35 yp-3 w131 r1 vg_vLeapModuleGUI_NameEdit hwndg_hLeapModuleGUI_NameEdit gLeapModuleGUI_NameEditProc, % SubStr(sLocGestures, 1, InStr(sLocGestures, "|") - 1)
GUI, Add, Button, xp+136 yp-2 w25 h25 hwndg_hLeapModuleGUI_DeleteBtn vg_vLeapModuleGUI_DeleteBtn gLeapModuleGUI_DeleteBtn, `n`n`n&-
ILButton(g_hLeapModuleGUI_DeleteBtn, g_sLeapWorkingDir "\Delete.ico")
GUI, Add, Button, xp+29 yp wp hp hwndg_hLeapModuleGUI_AddBtn vg_vLeapModuleGUI_AddBtn gLeapModuleGUI_AddBtn, `n`n`n&=
ILButton(g_hLeapModuleGUI_AddBtn, g_sLeapWorkingDir "\Add.ico")
GUI, Add, ListBox, % "xp-201 yp+" s_iMSDNStandardBtnH+2 " w225 r13 vg_vLeapModuleGUI_GestureLB hwndg_hLeapModuleGUI_GestureLB gLeapModuleGUI_GestureLBProc", % sLocGestures
;~ -------------------------------------------------------------------------------------------------------
;~ -------------------------------------------------------------------------------------------------------
GUI, Font, s12 c83B8G7
GUI, Add, GroupBox, x272 y40 h88 w237 Center, Press to insert gesture
GUI, Font, s8
g_sBtns := "Left|Right|Up|Down|Circle|KeyTap"
GUI, Add, Button, % "xp+" s_iMSDNStandardBtnSpacing " yp+" s_iMSDNStandardBtnH " w" s_iMSDNStandardBtnW " h" s_iMSDNStandardBtnH " gLeapModuleGUI_LeftBtn vg_vLeapModuleGUI_LeftBtn", &Left
GUI, Add, Button, xp+76 yp wp hp gLeapModuleGUI_RightBtn vg_vLeapModuleGUI_RightBtn, &Right
GUI, Add, Button, xp+76 yp wp hp gLeapModuleGUI_UpBtn vg_vLeapModuleGUI_UpBtn, &Up
GUI, Add, Button, % "xp-152 yp+" s_iMSDNStandardBtnH + s_iMSDNStandardBtnSpacing " wp hp gLeapModuleGUI_DownBtn vg_vLeapModuleGUI_DownBtn", &Down
GUI, Add, Button, xp+76 yp wp hp gLeapModuleGUI_CircleBtn vg_vLeapModuleGUI_CircleBtn, C&ircle
GUI, Add, Button, xp+76 yp wp hp gLeapModuleGUI_KeyTapBtn vg_vLeapModuleGUI_KeyTapBtn, &KeyTap
;~ -------------------------------------------------------------------------------------------------------
GUI, Font, s12 c83B8G7
GUI, Add, Edit, xp-158 yp+44 r6 w409 ReadOnly -TabStop vg_vLeapModuleGUI_RecordOutputEdit
GUI, Font, s8
GUI, Add, Button, xp+415 yp-1 w%s_iMSDNStandardBtnW% h%s_iMSDNStandardBtnH% vg_vLeapModuleGUI_RecordBtn gLeapModuleGUI_RecordBtn, R&ecord
GUI, Add, Button, xp yp+53 wp hp gLeapModuleGUI_ClearBtn, Cle&ar
GUI, Add, Button, xp yp+54 wp hp gLeapModuleGUI_SaveBtn, &Save
; Hotkeys
OnMessage(WM_KEYDOWN:=256, "LeapModuleGUI_OnKeyDown")
Hotkey, IfWinActive, ahk_id %g_hLeapModuleGUI%
Hotkey, ^S, LeapModuleGUI_SaveBtn
return this
LeapModuleGUI_NameEditProc:
{
LeapModuleGUI_NameEditProc()
return
}
LeapModuleGUI_AddBtn:
{
; Retrieve the name of the gesture to be added...
GUIControlGet, sGestureName,, g_vLeapModuleGUI_NameEdit
; Clear the output, since that is specific to whatever gesture is selected in the LB...
GUIControl,, g_vLeapModuleGUI_RecordOutputEdit
; Add the gesture to the LB
GUIControl,, g_vLeapModuleGUI_GestureLB, %sGestureName%
; Select the new gesture in the LB
SendMessage, LB_GETCOUNT:=395, 0, 0,, ahk_id %g_hLeapModuleGUI_GestureLB%
GUIControl, Choose, g_vLeapModuleGUI_GestureLB, % ErrorLevel
; Since we are selected on the gesture in the LB, we shouldn't clear the edit
; Select all the text in the edit so that it is easy to create a new gesture
SendMessage, 177, 0, -1, , % "ahk_id" g_hLeapModuleGUI_NameEdit ; EM_SETSEL:=177
; We forced a selection in the LB, so do LBProc
LeapModuleGUI_GestureLBProc()
GUIControl, Focus, g_vLeapModuleGUI_NameEdit
; Finally, add gesture to ini
g_vGesturesIni.AddSection(sGestureName)
return
}
LeapModuleGUI_DeleteBtn:
{
LeapModuleGUI_DeleteBtn()
return
}
LeapModuleGUI_GestureLBProc:
{
LeapModuleGUI_GestureLBProc()
return
}
LeapModuleGUI_LeftBtn:
LeapModuleGUI_RightBtn:
LeapModuleGUI_UpBtn:
LeapModuleGUI_DownBtn:
LeapModuleGUI_CircleBtn:
LeapModuleGUI_KeyTapBtn:
{
GUIControlGet, sOutput,, g_vLeapModuleGUI_RecordOutputEdit
sInput := SubStr(A_ThisLabel, 15)
sInput := SubStr(sInput, 1, StrLen(sInput) - 3)
LeapModuleGUI_EnableDisableBtns(sInput)
if (sOutput != A_Blank)
sInput := sOutput ", " sInput
GUIControl,, g_vLeapModuleGUI_RecordOutputEdit, % sInput
g_vGesturesIni[sGesture].Gesture := sInput
sInput :=
return
}
LeapModuleGUI_ClearBtn:
{
GUIControl,, g_vLeapModuleGUI_RecordOutputEdit
g_vGesturesIni[sGesture].Gesture := A_Blank
LeapModuleGUI_EnableDisableBtns()
return
}
LeapModuleGUI_SaveBtn:
{
g_vGesturesIni.Save()
GUI LeapModuleGUI_: +OwnDialogs
Msgbox, 8192,, Saved!
return
}
LeapModuleGUI_OKBtn:
g_vGesturesIni.Save()
LeapModuleGUI_GUIEscape:
LeapModuleGUI_GUIClose:
{
GUI, LeapModuleGUI_:Hide
if (g_hLeapModuleGUIOwner)
{
GUI, LeapModuleGUI_:-Owner%g_hLeapModuleGUIOwner%
WInset, Enable,, ahk_id %g_hLeapModuleGUIOwner%
WinActivate, ahk_id %g_hLeapModuleGUIOwner%
}
return
}
}
;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;
__Delete()
{
this.m_vLeap :=
GUI, LeapModuleGUI_:Destroy
return
}
;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;
ShowSettingsDlg(hOwner=0)
{
GUI, LeapModuleGUI_:Default
this.m_vGesturesIni := g_vGesturesIni := class_EasyIni(this.m_vGesturesIni.EasyIni_ReservedFor_m_sFile) ; Need to be sure that ini is up-to-date!
if (hOwner)
{
global g_hLeapModuleGUIOwner := hOwner
GUI, +Owner%g_hLeapModuleGUIOwner%
WinSet, Disable,, ahk_id %g_hLeapModuleGUIOwner%
}
GUI, Show, AutoSize
; Select and focus the first item in the LB
GUIControl, Choose, g_vLeapModuleGUI_GestureLB, 1
LeapModuleGUI_GestureLBProc()
GUIControl, Focus, g_vLeapModuleGUI_NameEdit
return
}
;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
}
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;
LeapModuleGUI_OnKeyDown(wParam, lParam, msg, hWnd)
{
global
if (hWnd == g_hLeapModuleGUI_NameEdit)
{
if (wParam == GetKeyVK("Up"))
{
SendMessage, 392, 0, 0, , % "ahk_id" g_hLeapModuleGUI_GestureLB ; LB_GETCURSEL:=392
SendMessage, 390, ErrorLevel <= 0 ? 0 : ErrorLevel - 1, 0, , % "ahk_id" g_hLeapModuleGUI_GestureLB ; LB_SETCURSEL:=390
LeapModuleGUI_GestureLBProc()
return 0 ; Prevents Edit from acting on WM_KeyDown
}
else if (wParam == GetKeyVK("Down"))
{
SendMessage, 392, 0, 0, , % "ahk_id" g_hLeapModuleGUI_GestureLB ; LB_GETCURSEL:=392
SendMessage, 390, ErrorLevel + 1, 0, , % "ahk_id" g_hLeapModuleGUI_GestureLB ; LB_SETCURSEL:=390
LeapModuleGUI_GestureLBProc()
return 0 ; Prevents Edit from acting on WM_KeyDown
}
}
if (wParam == GetKeyVK("Delete"))
{
LeapModuleGUI_DeleteBtn()
LeapModuleGUI_GestureLBProc()
return 0 ; Prevents Edit from acting on WM_KeyDown
}
return
}
;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;
LeapModuleGUI_GestureLBProc()
{
global
GUI, LeapModuleGUI_:Default
ControlGet, sGesture, Choice,,, % "ahk_id" g_hLeapModuleGUI_GestureLB
GUIControl,, g_vLeapModuleGUI_NameEdit, %sGesture%
GUIControl,, g_vLeapModuleGUI_RecordOutputEdit, % g_vGesturesIni[sGesture].Gesture
SendMessage, 177, 0, -1, , % "ahk_id" g_hLeapModuleGUI_NameEdit ; EM_SETSEL:=177
; When an existing gesture is loaded, then the final gesture, such as left,
; needs its associated button disabled
LeapModuleGUI_EnableDisableBtns(SubStr(g_vGesturesIni[sGesture].Gesture, InStr(g_vGesturesIni[sGesture].Gesture, ",", false, -1) + 2))
return
}
;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;
LeapModuleGUI_DeleteBtn()
{
global
GUI, LeapModuleGUI_:Default
SendMessage, 392, ErrorLevel, 0, , % "ahk_id" g_hLeapModuleGUI_GestureLB ; LB_GETCURSEL:=392
iLastSel := ErrorLevel
SendMessage, 386, iLastSel, 0, , % "ahk_id" g_hLeapModuleGUI_GestureLB ; LB_DELETESTRING:=386
GUIControl,, g_vLeapModuleGUI_NameEdit
GUIControl, Choose, g_vLeapModuleGUI_GestureLB, % iLastSel <= 0 ? 1 : iLastSel
LeapModuleGUI_GestureLBProc()
GUIControl, Focus, g_vLeapModuleGUI_NameEdit
g_vGesturesIni.DeleteSection(sGestureName)
LeapModuleGUI_EnableDisableBtns(sInput)
return
}
;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;
LeapModuleGUI_NameEditProc()
{
global
GUIControlGet, sGestureName,, g_vLeapModuleGUI_NameEdit
sGestureName := Trim(sGestureName)
if (g_vGesturesIni.HasKey(sGestureName))
{
GUIControl, Disable, g_vLeapModuleGUI_AddBtn
; Update the output with this gesture
GUIControl,, g_vLeapModuleGUI_RecordOutputEdit, % g_vGesturesIni[sGestureName].Gesture
LeapModuleGUI_EnableDisableBtns(LeapModuleGUI_GetLastGesture())
}
else
{
GUIControl, Enable, g_vLeapModuleGUI_AddBtn
; Clear the output
GUIControl,, g_vLeapModuleGUI_RecordOutputEdit,
LeapModuleGUI_EnableDisableBtns()
}
return
}
;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;
LeapModuleGUI_EnableDisableBtns(sBtnToDisable="")
{
global
Loop, Parse, g_sBtns, |
GUIControl, % (sBtnToDisable = A_LoopField ? "Disable" : "Enable"), % "g_vLeapModuleGUI_" A_LoopField "Btn"
return
}
;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;
LeapModuleGUI_GetLastGesture()
{
global
GUIControlGet, sGestureName,, g_vLeapModuleGUI_NameEdit
sGestureName := Trim(sGestureName)
if (iPosOfLastComma := InStr(g_vGesturesIni[sGestureName].Gesture, ",", false, -1))
return SubStr(g_vGesturesIni[sGestureName].Gesture, iPosOfLastComma + 2)
else return g_vGesturesIni[sGestureName].Gesture
}
;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;
LeapMsgHandler(sMsg, asGestures, ByRef rsOutput)
{
global g_bRecord, g_vLeapModuleGUI_RecordOutputEdit, sInput, g_hMsgHandler
GUI, LeapModuleGUI_:Default
; If we are just recording, update the GUI
; else if we are NOT recording, process the gesture
if (g_bRecord)
{
if (sMsg = "Record")
{
GUIControl,, g_vLeapModuleGUI_RecordOutputEdit, % st_glue(asGestures, ", ")
LeapModuleGUI_EnableDisableBtns(SubStr(asGestures[asGestures.MaxIndex()], InStr(asGestures[asGestures.MaxIndex()], ",", false, -1) + 1))
}
}
else if (sMsg = "Post" && g_hMsgHandler)
g_hMsgHandler.(sMsg, asGestures, rsOutput) ;~ DoActionFromGesture(asGestures, rsOutput)
return
}
;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;
LeapModuleGUI_RecordBtn:
{
; Toggle
g_bRecord := !g_bRecord
if (g_bRecord)
{
GUIControl,, g_vLeapModuleGUI_RecordBtn, Paus&e
GUIControl,, g_vLeapModuleGUI_RecordOutputEdit
}
else GUIControl,, g_vLeapModuleGUI_RecordBtn, R&ecord
return
}
;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;; asGestures is guaranteed to be populated
;;;;;;;;;;;;;; rsOutput is text to be outputted on the OSD
DoActionFromGesture(asGestures, ByRef rsOutput)
{
Msgbox oh no!
WinGetPos, iX, iY, iW, iH, A
sGesture := st_glue(asGestures, ",")
if (sGesture = "Left")
{
WinMove, A,, iX - A_ScreenWidth
rsOutput = "Move to left monitor"
}
else if (sGesture = "Right")
{
WinMove, A,, iX + A_ScreenWidth
rsOutput = "Move to right monitor"
}
else if (sGesture = "Up")
{
WinMaximize, A
rsOutput = "Maximize"
}
else if (sGesture = "Down")
{
WinMinimize, A
rsOutput = "Minimize"
}
else if (sGesture = "Circle")
{
WinMove, A,, (A_ScreenWidth - iW) / 2, (A_ScreenHeight - iH) / 2
rsOutput = "Center!"
}
else if (sGesture = "Down,Left")
{
rsOutput := "Snap to bottom left"
WinMove, A,, 0, A_ScreenHeight - iH
}
else if (sGesture = "Down,Right")
{
rsOutput := "Snap to bottom right"
WinMove, A,, A_ScreenWidth - iW, A_ScreenHeight - iH
}
else if (sGesture = "Up,Left")
{
rsOutput := "Snap to top left"
WinMove, A,, 0, 0
}
else if (sGesture = "Up,Right")
{
rsOutput := "Snap to top right"
WinMove, A,, A_ScreenWidth - iW, 0
}
return
}
;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
#Include %A_ScriptDir%\AutoLeap\AutoLeap.ahk
#Include %A_ScriptDir%\AutoLeap\ILButton.ahk
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment