From 14fc3fec3eecce2a471160bd5c7e9b7e982b84d6 Mon Sep 17 00:00:00 2001 From: Alexander Bibanin <32kb@bk.ru> Date: Thu, 29 Sep 2016 01:28:42 +0300 Subject: [PATCH 1/2] Added support for global hotkeys + example --- examples/globalhotkey/globalhotkey.go | 50 ++++++++++++++++++++ examples/globalhotkey/globalhotkey.manifest | 14 ++++++ examples/globalhotkey/rsrc.syso | Bin 0 -> 918 bytes globalhotkey.go | 32 +++++++++++++ hotkeyevent.go | 45 ++++++++++++++++++ window.go | 18 +++++++ 6 files changed, 159 insertions(+) create mode 100644 examples/globalhotkey/globalhotkey.go create mode 100644 examples/globalhotkey/globalhotkey.manifest create mode 100644 examples/globalhotkey/rsrc.syso create mode 100644 globalhotkey.go create mode 100644 hotkeyevent.go diff --git a/examples/globalhotkey/globalhotkey.go b/examples/globalhotkey/globalhotkey.go new file mode 100644 index 000000000..3328142b6 --- /dev/null +++ b/examples/globalhotkey/globalhotkey.go @@ -0,0 +1,50 @@ +package main + +import ( + "github.com/lxn/walk" + . "github.com/lxn/walk/declarative" +) + +func main() { + + var ( + e error + winMain *walk.MainWindow + label *walk.Label + ) + + winMain, e = walk.NewMainWindow() + if e != nil { panic(e) } + + // Define hotkey handler + winMain.Hotkey().Attach(func(hkid int) { + label.SetText("") + switch hkid { + case 1: + label.SetText("Global hotkey 1 pressed: Ctrl+Alt+X") + case 2: + label.SetText("Global hotkey 2 pressed: Alt+Shift+D") + } + }) + + // Register hotkeys globally + walk.RegisterGlobalHotKey(winMain, 1, walk.Shortcut{Modifiers: walk.ModControl | walk.ModAlt, Key: walk.KeyX}) + walk.RegisterGlobalHotKey(winMain, 2, walk.Shortcut{Modifiers: walk.ModShift | walk.ModAlt, Key: walk.KeyD}) + + MainWindow { + AssignTo: &winMain, + Size: Size{400, 120}, + Layout: VBox{}, + Children: []Widget { + Label { + Text: "Focus on another window and press Ctrl+Alt+X or Alt+Shift+D", + }, + Label { + AssignTo: &label, + Text: "-", + Font: Font{PointSize: 12}, + }, + }, + }.Run() + +} diff --git a/examples/globalhotkey/globalhotkey.manifest b/examples/globalhotkey/globalhotkey.manifest new file mode 100644 index 000000000..7936c0e72 --- /dev/null +++ b/examples/globalhotkey/globalhotkey.manifest @@ -0,0 +1,14 @@ + + + + + + + + + + + true + + + diff --git a/examples/globalhotkey/rsrc.syso b/examples/globalhotkey/rsrc.syso new file mode 100644 index 0000000000000000000000000000000000000000..e33cf240f187a01dc58f86037bb0e9526cff982d GIT binary patch literal 918 zcmah|&2HN;40hH6Lk8@=qoLz~*iQ3<&2ZWU1&S71cS-kiRb(a+YDtD9C-bfEwZq<} zFVI&QCEHB{1RG&lB1P%rM^ayUy}-%CNf0!v(_W;FX5QNTQrydMxy9Lu^ZVpKr*+Z` z-r*9*LuYB`p7&3V*!!nJaE(qsaXh}-pL+WJC%gD1if{|sa3yDC7$qbG$yCnedPct8 zT@5~v&{!&SS||xKQiCBE=WpL+)EE%Uq7Lx_X=bF-GBs=if|`NgOe>>ScEFTKsS(k3 zNJ2p+UxBgT577K^=d&IC_mUAoTOY(9C5}U>6$4|Go@=(@7MQIxM*p6K zk_ucblz^*B-q&AIy#@^=v~`IaJ1$3OB=$Htlpqm<)lThaoo?qXcW*ut&&#V?z}qe* z(GMw!jL_SspIf(7G1Xj#0(*1A6Vr>eNNHIj0=3u|4g1E7`Ek)Nz*=k((^Y?CZJEY#+W^sX z14P&W@#6L-K1-5G+*0oj`)azpmVCaW8qTe*0K@52pY-@BuCGtKbr@-e)}}Mj$927v Q*TKuT=HAswfR7^h3t>J2CIA2c literal 0 HcmV?d00001 diff --git a/globalhotkey.go b/globalhotkey.go new file mode 100644 index 000000000..281e325c0 --- /dev/null +++ b/globalhotkey.go @@ -0,0 +1,32 @@ +// Copyright 2010 The Walk Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// +build windows + +package walk + +import ( + "github.com/lxn/win" +) + +func RegisterGlobalHotKey(owner Form, hkid int, hKey Shortcut) bool { + var ownerHWnd win.HWND + + if owner != nil { + ownerHWnd = owner.Handle() + } + + var modifiers uint + if hKey.Modifiers & ModAlt != 0 { + modifiers |= 1 + } + if hKey.Modifiers & ModControl != 0 { + modifiers |= 2 + } + if hKey.Modifiers & ModShift != 0 { + modifiers |= 4 + } + // https://msdn.microsoft.com/ru-ru/library/windows/desktop/ms646309.aspx + return win.RegisterHotKey(ownerHWnd, hkid, modifiers, uint(hKey.Key)) +} diff --git a/hotkeyevent.go b/hotkeyevent.go new file mode 100644 index 000000000..2b5b97827 --- /dev/null +++ b/hotkeyevent.go @@ -0,0 +1,45 @@ +// Copyright 2011 The Walk Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// +build windows + +package walk + +type HotkeyEventHandler func(hkid int) + +type HotkeyEvent struct { + handlers []HotkeyEventHandler +} + +func (e *HotkeyEvent) Attach(handler HotkeyEventHandler) int { + for i, h := range e.handlers { + if h == nil { + e.handlers[i] = handler + return i + } + } + + e.handlers = append(e.handlers, handler) + return len(e.handlers) - 1 +} + +func (e *HotkeyEvent) Detach(handle int) { + e.handlers[handle] = nil +} + +type HotkeyEventPublisher struct { + event HotkeyEvent +} + +func (p *HotkeyEventPublisher) Event() *HotkeyEvent { + return &p.event +} + +func (p *HotkeyEventPublisher) Publish(hkid int) { + for _, handler := range p.event.handlers { + if handler != nil { + handler(hkid) + } + } +} diff --git a/window.go b/window.go index baafb703f..43a98f184 100644 --- a/window.go +++ b/window.go @@ -124,6 +124,10 @@ type Window interface { // events for the Window. KeyUp() *KeyEvent + // Hotkey returns a *HotkeyEvent that you can attach to for handling global + // hotkey events for the Window. + Hotkey() *HotkeyEvent + // MaxSize returns the maximum allowed outer Size for the Window, including // decorations. // @@ -284,6 +288,7 @@ type WindowBase struct { keyDownPublisher KeyEventPublisher keyPressPublisher KeyEventPublisher keyUpPublisher KeyEventPublisher + hotkeyPublisher HotkeyEventPublisher mouseDownPublisher MouseEventPublisher mouseUpPublisher MouseEventPublisher mouseMovePublisher MouseEventPublisher @@ -1192,6 +1197,12 @@ func (wb *WindowBase) KeyUp() *KeyEvent { return wb.keyUpPublisher.Event() } +// Hotkey returns a *HotkeyEvent that you can attach to for handling global +// hotkey events for the *WindowBase. +func (wb *WindowBase) Hotkey() *HotkeyEvent { + return wb.hotkeyPublisher.Event() +} + // DropFiles returns a *DropFilesEvent that you can attach to for handling // drop file events for the *WindowBase. func (wb *WindowBase) DropFiles() *DropFilesEvent { @@ -1371,6 +1382,10 @@ func (wb *WindowBase) handleKeyUp(wParam, lParam uintptr) { wb.keyUpPublisher.Publish(Key(wParam)) } +func (wb *WindowBase) handleHotkey(wParam, lParam uintptr) { + wb.hotkeyPublisher.Publish(int(wParam)) +} + // WndProc is the window procedure of the window. // // When implementing your own WndProc to add or modify behavior, call the @@ -1470,6 +1485,9 @@ func (wb *WindowBase) WndProc(hwnd win.HWND, msg uint32, wParam, lParam uintptr) case win.WM_KEYUP: wb.handleKeyUp(wParam, lParam) + case win.WM_HOTKEY: + wb.handleHotkey(wParam, lParam) + case win.WM_DROPFILES: wb.dropFilesPublisher.Publish(win.HDROP(wParam)) From 9789536f9702a1aa5dca9e8c82cce8e5852475f6 Mon Sep 17 00:00:00 2001 From: Bibainet <32kb@bk.ru> Date: Thu, 14 Sep 2017 23:15:00 +0300 Subject: [PATCH 2/2] Add .gitattributes --- .gitattributes | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 .gitattributes diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 000000000..5572e69ea --- /dev/null +++ b/.gitattributes @@ -0,0 +1,16 @@ +* eol=lf + +*.cmd eol=crlf +*.bat eol=crlf + +*.syso binary +*.gif binary +*.png binary +*.jpg binary +*.ico binary +*.zip binary +*.ttf binary +*.otf binary +*.woff binary +*.eot binary +*.pdf binary