-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(linux): expose XSetErrorHandler
- Loading branch information
1 parent
b950a6e
commit 16e8db4
Showing
3 changed files
with
142 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Copyright 2022 The golang.design Initiative Authors. | ||
// All rights reserved. Use of this source code is governed | ||
// by a MIT license that can be found in the LICENSE file. | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"sync" | ||
|
||
"golang.design/x/hotkey" | ||
"golang.design/x/hotkey/mainthread" | ||
) | ||
|
||
func main() { mainthread.Init(fn) } | ||
func fn() { | ||
wg := sync.WaitGroup{} | ||
wg.Add(2) | ||
|
||
hotkey.XSetErrorHandler(func(e hotkey.XErrorEvent) { | ||
fmt.Printf("X error: %v\n", e) | ||
}) | ||
|
||
go func() { | ||
defer wg.Done() | ||
|
||
err := listenHotkey(hotkey.KeyS, hotkey.ModCtrl, hotkey.ModShift) | ||
if err != nil { | ||
log.Println(err) | ||
} | ||
}() | ||
go func() { | ||
defer wg.Done() | ||
|
||
err := listenHotkey(hotkey.KeyA, hotkey.ModCtrl, hotkey.ModShift) | ||
if err != nil { | ||
log.Println(err) | ||
} | ||
}() | ||
go func() { | ||
defer wg.Done() | ||
|
||
err := listenHotkey(hotkey.KeyA, hotkey.ModCtrl, hotkey.ModShift) | ||
if err != nil { | ||
log.Println(err) | ||
} | ||
}() | ||
wg.Wait() | ||
|
||
hotkey.XRestoreErrorHandler() | ||
|
||
// Will now crash with BadAccess | ||
wg.Add(2) | ||
go func() { | ||
defer wg.Done() | ||
|
||
err := listenHotkey(hotkey.KeyA, hotkey.ModCtrl, hotkey.ModShift) | ||
if err != nil { | ||
log.Println(err) | ||
} | ||
}() | ||
go func() { | ||
defer wg.Done() | ||
|
||
err := listenHotkey(hotkey.KeyA, hotkey.ModCtrl, hotkey.ModShift) | ||
if err != nil { | ||
log.Println(err) | ||
} | ||
}() | ||
wg.Wait() | ||
} | ||
|
||
func listenHotkey(key hotkey.Key, mods ...hotkey.Modifier) (err error) { | ||
ms := []hotkey.Modifier{} | ||
ms = append(ms, mods...) | ||
hk := hotkey.New(ms, key) | ||
|
||
err = hk.Register() | ||
if err != nil { | ||
return | ||
} | ||
|
||
// Blocks until the hokey is triggered. | ||
<-hk.Keydown() | ||
log.Printf("hotkey: %v is down\n", hk) | ||
<-hk.Keyup() | ||
log.Printf("hotkey: %v is up\n", hk) | ||
hk.Unregister() | ||
return | ||
} |
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