-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto-type hotkeys only work inside Starbase
- Loading branch information
1 parent
d83340b
commit f449369
Showing
4 changed files
with
111 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// +build windows | ||
|
||
package win32 | ||
|
||
import ( | ||
"context" | ||
"syscall" | ||
"time" | ||
"unsafe" | ||
) | ||
|
||
var ( | ||
forgrWindow = user32.MustFindProc("GetForegroundWindow") | ||
windowText = user32.MustFindProc("GetWindowTextW") | ||
windowTextLen = user32.MustFindProc("GetWindowTextLengthW") | ||
) | ||
|
||
// GetForegroundWindow returns the name of the window that currently has the user-focus | ||
func GetForegroundWindow() string { | ||
hwnd, _, _ := forgrWindow.Call() | ||
|
||
if hwnd != 0 { | ||
textlen, _, _ := windowTextLen.Call(hwnd) | ||
buf := make([]uint16, textlen+1) | ||
windowText.Call(hwnd, uintptr(unsafe.Pointer(&buf[0])), uintptr(textlen+1)) | ||
return syscall.UTF16ToString(buf) | ||
} | ||
|
||
return "" | ||
} | ||
|
||
// WaitForWindowChange blocks until the window with the user-focus changes (or ctx is cancelled). Returns the title of the new active window | ||
func WaitForWindowChange(ctx context.Context) string { | ||
oldWindow := GetForegroundWindow() | ||
for { | ||
time.Sleep(500 * time.Millisecond) | ||
newWindow := GetForegroundWindow() | ||
if newWindow != oldWindow { | ||
return newWindow | ||
} | ||
if ctx != nil && ctx.Err() != nil { | ||
return oldWindow | ||
} | ||
|
||
} | ||
} |