diff options
| author | jEzEk <[email protected]> | 2021-03-07 15:15:21 +0100 |
|---|---|---|
| committer | GitHub <[email protected]> | 2021-03-07 15:15:21 +0100 |
| commit | b58285defde79d5d7952f6e22a0f310adb797b64 (patch) | |
| tree | d44231b4ba5dc85a087ebf5460a8efb54607d4e5 /examples/create-window | |
| parent | ddeaff51cf5f951927d5ec1ddf08d8c67e6bc1d8 (diff) | |
| parent | 06eab223430e738079eb644911256671a319980e (diff) | |
Merge pull request #1 from scrouthtv/master
Added an explanation of how to use events to the create-window example
Diffstat (limited to 'examples/create-window')
| -rw-r--r-- | examples/create-window/main.go | 43 |
1 files changed, 42 insertions, 1 deletions
diff --git a/examples/create-window/main.go b/examples/create-window/main.go index 50a9bff..a637d0e 100644 --- a/examples/create-window/main.go +++ b/examples/create-window/main.go @@ -17,6 +17,7 @@ func main() { fmt.Println(err) return } + defer X.Close() // xproto.Setup retrieves the Setup information from the setup bytes // gathered during connection. @@ -49,7 +50,8 @@ func main() { 0xffffffff, xproto.EventMaskStructureNotify | xproto.EventMaskKeyPress | - xproto.EventMaskKeyRelease}) + xproto.EventMaskKeyRelease, + }) // MapWindow makes the window we've created appear on the screen. // We demonstrated the use of a 'checked' request here. @@ -103,5 +105,44 @@ func main() { if xerr != nil { fmt.Printf("Error: %s\n", xerr) } + + // This is how accepting events work: + // The application checks what event we got and reacts to it + // accordingly. All events are defined in the xproto subpackage. + // To receive events, we have to first register it using + // either xproto.CreateWindow or xproto.ChangeWindowAttributes. + switch ev.(type) { + case xproto.KeyPressEvent: + // See https://pkg.go.dev/github.com/jezek/xgb/xproto#KeyPressEvent + // for documentation about a key press event. + kpe := ev.(xproto.KeyPressEvent) + fmt.Printf("Key pressed: %d\n", kpe.Detail) + // The Detail value depends on the keyboard layout, + // for QWERTY, q is #24. + if kpe.Detail == 24 { + return // exit on q + } + case xproto.DestroyNotifyEvent: + // Depending on the user's desktop environment (especially + // window manager), killing a window might close the + // client's X connection (e. g. the default Ubuntu + // desktop environment). + // + // If that's the case for your environment, closing this example's window + // will also close the underlying Go program (because closing the X + // connection gives a nil event and EOF error). + // + // Consider how a single application might have multiple windows + // (e.g. an open popup or dialog, ...) + // + // With other DEs, the X connection will still stay open even after the + // X window is closed. For these DEs (e.g. i3) we have to check whether + // the WM sent us a DestroyNotifyEvent and close our program. + // + // For more information about closing windows while maintaining + // the X connection see + // https://github.com/jezek/xgbutil/blob/master/_examples/graceful-window-close/main.go + return + } } } |
