Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #SingleInstance, Force
- #Hotstring *1 B0
- #LTrim
- ::shape::
- TextExpandList("
- (
- triangle
- square
- pentagon
- hexagon
- heptagon
- octagon
- nonagon
- decagon
- item 9
- item 10
- item 11
- item 12
- item 13
- item 14
- item 15
- item 16
- item 17
- item 18
- item 19
- item 20
- )")
- Return
- ; example
- ::Num::
- TextExpandList("
- (
- one
- two
- three
- four
- five
- six
- )")
- Return
- #Hotstring *0 B1
- #LTrim Off
- ; ----------------------------------------------------------------------------------------
- #If WinExist("ahk_id" TextExpandList.GuiHwnd)
- Tab::TextExpandList.SelectDown(10)
- +Down::TextExpandList.SelectDown(5)
- NumpadDown::
- Down::
- TextExpandList.SelectDown(1)
- Return
- +Tab::TextExpandList.SelectUp(10)
- +Up::TextExpandList.SelectUp(5)
- NumpadUp::
- Up::
- TextExpandList.SelectUp(1)
- Return
- Enter::TextExpandList.Submit()
- Esc::TextExpandList.HideGui()
- #If
- TextExpandList(MatchList) {
- TextExpandList.ShowGui(MatchList)
- }
- ; ----------------------------------------------------------------------------------------
- class TextExpandList {
- static Title := "Custom Text Expander"
- , TypeSize := 12
- , TypeFace := "Segoe UI"
- , AutoInsertSpace := True
- ShowGui(MatchList) {
- static GuiCreated, ItemHeight
- ; Make new gui if not created yet
- if !GuiCreated {
- static WS_EX_COMPOSITED := 0x2000000
- static WS_EX_LAYERED := 0x0080000
- static DOUBLE_BUFFER := WS_EX_COMPOSITED | WS_EX_LAYERED
- Gui New, -Caption +ToolWindow +AlwaysOnTop +hwndGuiHwnd -DPIScale +E%DOUBLE_BUFFER%, % this.Title
- ; hwnd of Gui root window
- this.GuiHwnd := GuiHwnd
- Gui font, % "s" this.TypeSize, % this.TypeFace
- Gui Margin, 0, 0
- ; AltSubmit makes listbox save position instead of name
- Gui Add, ListBox, VScroll x0 y0 +hwndHLB +AltSubmit
- ; hwnd to listbox
- this.HLB := HLB
- ; get height of a single item in listbox
- ItemHeight := this.LB_GetItemHeight(this.HLB)
- GuiCreated := True
- ih := this.KeyHook := InputHook("V")
- ih.KeyOpt("{All}", "+E")
- ih.KeyOpt("1234567890", "-E +SN")
- ih.KeyOpt("{LCtrl}{RCtrl}{LAlt}{RAlt}{LShift}{RShift}{LWin}{RWin}{NumLock}{CapsLock}{ScrollLock}{PrintScreen}", "-NE")
- ih.OnKeyDown := ObjBindMethod(TextExpandList, "OnKeyDown_TextExpandLB")
- ih.OnEnd := ObjBindMethod(TextExpandList, "OnEnd_TextExpandLB")
- }
- Gui % this.GuiHwnd ":Default"
- ; remember the hotstring used to trigger this function, remove the options and colons at the front
- this.HotstringText := RegExReplace(A_ThisHotkey, "^:.*:")
- ; Add numbers
- newList := ""
- Loop Parse, MatchList, `n, `r
- {
- ; put | between each item
- (newList != "") && newList .= "|"
- ; put the pos number in front of the current item
- newList .= A_Index (A_Index < 10 ? " " : " ")
- ; append each item
- newList .= A_LoopField
- ; pre-select the first item by adding a second |
- (A_Index = 1) && newList .= "|"
- }
- ; modify ListBox content
- ; you need the leading | to replace the content
- GuiControl, , % this.HLB, |%newList%
- ; get width
- MaxWidth := 0
- Loop Parse, newList, |
- {
- Width := this.TextWidth(A_LoopField, this.TypeFace, this.TypeSize)
- MaxWidth := Max(Width, MaxWidth)
- }
- ; add a little room to the end of the item
- MaxWidth += 20
- ; get height
- GuiControlGet, List, Pos, % this.HLB
- Items := this.LB_GetCount(this.HLB)
- ClientHeight := this.GetClientHeight(this.HLB)
- VisibleItems := ClientHeight // ItemHeight
- if (Items > 10) {
- Items := 10
- MaxWidth += 15
- }
- ListH += (Items - VisibleItems) * ItemHeight
- ; resize control
- GuiControl Move, % this.HLB, h%ListH% w%MaxWidth%
- caret := this.GetCaret()
- Gui Show, % "NA AutoSize x" caret.x " y" caret.y + 40
- this.KeyHook.Start()
- WinWaitClose % "ahk_id" this.GuiHwnd
- (this.KeyHook.InProgress) && this.KeyHook.Stop()
- }
- ; ----------------------------------------------------------------------------------------
- OnKeyDown_TextExpandLB(InputHook, VK, SC) {
- static chooseNum := ""
- static timeSinceLastKey := ""
- key := GetKeyName(Format("vk{:x}", VK) Format("sc{:x}", SC))
- if (A_TickCount - timeSinceLastKey < 400)
- chooseNum .= key
- else
- chooseNum := key
- LastItem := this.LB_GetCount(this.HLB)
- if (chooseNum > 0 && chooseNum <= LastItem)
- GuiControl Choose, % this.HLB, % chooseNum
- else if (chooseNum = 0) ; 0 chooses 10
- GuiControl Choose, % this.HLB, 10
- timeSinceLastKey := A_TickCount
- }
- OnEnd_TextExpandLB(InputHook) {
- this.HideGui()
- }
- ; ----------------------------------------------------------------------------------------
- ; function when item is selected
- Submit() {
- Gui % this.GuiHwnd ":Default"
- ; remove AltSubmit to get text instead of pos
- GuiControl -AltSubmit, % this.HLB
- GuiControlGet, LBText,, % this.HLB
- GuiControl +AltSubmit, % this.HLB
- GuiControlGet LBPos,, % this.HLB
- this.HideGui()
- ; use length of hotstring trigger to delete the hotstring word
- SendInput % "{BS " StrLen(this.HotstringText) "}"
- ; send the item selected from listbox
- ClipSend(RegExReplace(LBText, "^\d+ +"))
- if (this.AutoInsertSpace)
- SendInput % A_Space
- }
- ; ----------------------------------------------------------------------------------------
- ; function for selecting previous item
- ; item selection wraps around to last item if first item is selected when pressing up
- SelectUp(steps := 10) {
- Gui % this.GuiHwnd ":Default"
- GuiControlGet ItemPos,, % this.HLB
- LastItem := this.LB_GetCount(this.HLB)
- newPos := itemPos - steps
- if (newPos < 1)
- newPos := LastItem + 1 - steps
- GuiControl Choose, % this.HLB, % newPos
- }
- ; ----------------------------------------------------------------------------------------
- ; function for selecting next item
- ; item selection wraps around to first item if last item is selected when pressing up
- SelectDown(steps := 10) {
- Gui % this.GuiHwnd ":Default"
- GuiControlGet itemPos,, % this.HLB
- LastItem := this.LB_GetCount(this.HLB)
- newPos := itemPos + steps
- if (newPos > LastItem)
- newPos -= LastItem
- GuiControl Choose, % this.HLB, % newPos
- }
- ; ----------------------------------------------------------------------------------------
- HideGui() {
- Gui % this.GuiHwnd ":Hide"
- Hotstring("Reset")
- }
- ; ----------------------------------------------------------------------------------------
- GetClientHeight(HWND) { ; Retrieves the height of the client area.
- VarSetCapacity(RECT, 16, 0)
- DllCall("User32.dll\GetClientRect", "Ptr", HWND, "Ptr", &RECT)
- Return NumGet(RECT, 12, "Int")
- }
- ; ----------------------------------------------------------------------------------------
- LB_GetItemHeight(HLB) { ; Retrieves the height of a single list box item.
- ; LB_GETITEMHEIGHT = 0x01A1
- SendMessage, 0x01A1, 0, 0, , ahk_id %HLB%
- Return ErrorLevel
- }
- ; ----------------------------------------------------------------------------------------
- LB_GetCount(HLB) { ; Retrieves the number of items in the list box.
- ; LB_GETCOUNT = 0x018B
- SendMessage, 0x018B, 0, 0, , ahk_id %HLB%
- Return ErrorLevel
- }
- ; ----------------------------------------------------------------------------------------
- TextWidth(String, Typeface:="Segoe UI", Size:=12)
- {
- static hDC, hFont := 0, Extent
- If !hFont
- {
- hDC := DllCall("GetDC","UPtr",0,"UPtr")
- Height := -DllCall("MulDiv","Int",Size,"Int",DllCall("GetDeviceCaps","UPtr",hDC,"Int",90),"Int",72)
- hFont := DllCall("CreateFont","Int",Height,"Int",0,"Int",0,"Int",0,"Int",400,"UInt",False,"UInt",False,"UInt",False,"UInt",0,"UInt",0,"UInt",0,"UInt",0,"UInt",0,"Str",Typeface)
- hOriginalFont := DllCall("SelectObject","UPtr",hDC,"UPtr",hFont,"UPtr")
- VarSetCapacity(Extent,8)
- }
- DllCall("GetTextExtentPoint32","UPtr",hDC,"Str",String,"Int",StrLen(String),"UPtr",&Extent)
- Return, NumGet(Extent,0,"UInt")
- }
- ; ----------------------------------------------------------------------------------------
- GetCaret() {
- oc := A_CoordModeCaret
- CoordMode Caret, Screen
- ; get caret using A_CaretX and A_CaretY
- ; if A_CaretX and A_CaretY cannot be found, use acc method
- if (A_CaretX) {
- caret := { x: A_CaretX, y: A_CaretY }
- } else {
- Sleep 20
- activeHwnd := WinExist("A")
- oAcc := this.Acc_ObjectFromWindow(activeHwnd, OBJID_CARET := 0xFFFFFFF8)
- oAccCaret := this.Acc_Location(oAcc)
- caret := { x: oAccCaret.x, y: oAccCaret.y }
- }
- ; revert caret coordmode to original
- CoordMode Caret, % oc
- WinGetPos, X, Y, W, H, % "ahk_id " WinActive("A")
- ; no caret found?
- ; try using the location of the focused acc
- if (caret.x = 0) && (caret.y = 0) {
- oAcc := this.Acc_Location(this.Acc_Focus(activeHwnd))
- caret := {x: oAcc.x, y: oAcc.y}
- }
- return caret
- }
- ; ----------------------------------------------------------------------------------------
- ; https://www.autohotkey.com/boards/viewtopic.php?p=404749#p404749
- ; the accFocus "call" returns an Object if successful and an error code if not.
- ; So the while loop returns the last object were the accFocus call returns successfully.
- Acc_Focus(hWnd)
- {
- Acc := this.Acc_ObjectFromWindow(hWnd)
- While IsObject(Acc.accFocus)
- Acc := Acc.accFocus
- return Acc
- }
- ; ----------------------------------------------------------------------------------------
- Acc_WindowFromObject(pacc)
- {
- If DllCall("oleacc\WindowFromAccessibleObject", "Ptr", IsObject(pacc)?ComObjValue(pacc):pacc, "Ptr*", hWnd)=0
- Return hWnd
- }
- ; ----------------------------------------------------------------------------------------
- Acc_ObjectFromWindow(hWnd, idObject = -4)
- {
- static h := DllCall("LoadLibrary","Str","oleacc","Ptr")
- If DllCall("oleacc\AccessibleObjectFromWindow", "Ptr", hWnd, "UInt", idObject&=0xFFFFFFFF, "Ptr", -VarSetCapacity(IID,16)+NumPut(idObject==0xFFFFFFF0?0x46000000000000C0:0x719B3800AA000C81,NumPut(idObject==0xFFFFFFF0?0x0000000000020400:0x11CF3C3D618736E0,IID,"Int64"),"Int64"), "Ptr*", pacc)=0
- Return ComObjEnwrap(9,pacc,1)
- }
- ; ----------------------------------------------------------------------------------------
- Acc_Location(Acc, ChildId=0, byref Position="") { ; adapted from Sean's code
- try Acc.accLocation(ComObj(0x4003,&x:=0), ComObj(0x4003,&y:=0), ComObj(0x4003,&w:=0), ComObj(0x4003,&h:=0), ChildId)
- catch
- return
- Position := "x" NumGet(x,0,"int") " y" NumGet(y,0,"int") " w" NumGet(w,0,"int") " h" NumGet(h,0,"int")
- return {x:NumGet(x,0,"int"), y:NumGet(y,0,"int"), w:NumGet(w,0,"int"), h:NumGet(h,0,"int")}
- }
- }
- ClipSend(toSend) {
- static prevClip := ""
- prevClip := ClipboardAll
- Clipboard := ""
- Clipboard := toSend
- ClipWait, 1
- Send, {ctrl down}v{ctrl up}
- SetTimer, RevertClipboard, -100
- Return
- RevertClipboard:
- Clipboard := prevClip
- prevClip := ""
- Return
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement