io/key: add ModAlt, ModSuper

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2019-11-08 18:21:43 +01:00
parent c4416ffbc4
commit c833c98fd7
6 changed files with 45 additions and 1 deletions
+4 -1
View File
@@ -210,8 +210,11 @@ func (w *window) keyEvent(e js.Value) {
k := e.Get("key").String() k := e.Get("key").String()
if n, ok := translateKey(k); ok { if n, ok := translateKey(k); ok {
cmd := key.Event{Name: n} cmd := key.Event{Name: n}
if e.Call("getModifierState", "Alt").Bool() {
cmd.Modifiers |= key.ModAlt
}
if e.Call("getModifierState", "Control").Bool() { if e.Call("getModifierState", "Control").Bool() {
cmd.Modifiers |= key.ModCommand cmd.Modifiers |= key.ModCtrl
} }
if e.Call("getModifierState", "Shift").Bool() { if e.Call("getModifierState", "Shift").Bool() {
cmd.Modifiers |= key.ModShift cmd.Modifiers |= key.ModShift
+6
View File
@@ -119,6 +119,12 @@ func gio_onFrameCallback(view C.CFTypeRef) {
func gio_onKeys(view C.CFTypeRef, cstr *C.char, ti C.double, mods C.NSUInteger) { func gio_onKeys(view C.CFTypeRef, cstr *C.char, ti C.double, mods C.NSUInteger) {
str := C.GoString(cstr) str := C.GoString(cstr)
var kmods key.Modifiers var kmods key.Modifiers
if mods&C.NSAlternateKeyMask != 0 {
kmods |= key.ModAlt
}
if mods&C.NSControlKeyMask != 0 {
kmods |= key.ModCtrl
}
if mods&C.NSCommandKeyMask != 0 { if mods&C.NSCommandKeyMask != 0 {
kmods |= key.ModCommand kmods |= key.ModCommand
} }
+9
View File
@@ -94,6 +94,9 @@ const (
_USER_TIMER_MINIMUM = 0x0000000A _USER_TIMER_MINIMUM = 0x0000000A
_VK_CONTROL = 0x11 _VK_CONTROL = 0x11
_VK_LWIN = 0x5B
_VK_MENU = 0x12
_VK_RWIN = 0x5C
_VK_SHIFT = 0x10 _VK_SHIFT = 0x10
_VK_BACK = 0x08 _VK_BACK = 0x08
@@ -280,6 +283,12 @@ func windowProc(hwnd syscall.Handle, msg uint32, wParam, lParam uintptr) uintptr
case _WM_KEYDOWN, _WM_SYSKEYDOWN: case _WM_KEYDOWN, _WM_SYSKEYDOWN:
if n, ok := convertKeyCode(wParam); ok { if n, ok := convertKeyCode(wParam); ok {
cmd := key.Event{Name: n} cmd := key.Event{Name: n}
if getKeyState(_VK_LWIN)&0x1000 != 0 || getKeyState(_VK_RWIN)&0x1000 != 0 {
cmd.Modifiers |= key.ModSuper
}
if getKeyState(_VK_MENU)&0x1000 != 0 {
cmd.Modifiers |= key.ModAlt
}
if getKeyState(_VK_CONTROL)&0x1000 != 0 { if getKeyState(_VK_CONTROL)&0x1000 != 0 {
cmd.Modifiers |= key.ModCtrl cmd.Modifiers |= key.ModCtrl
} }
+6
View File
@@ -334,6 +334,12 @@ func (h *x11EventHandler) handleEvents() bool {
func x11KeyStateToModifiers(s C.uint) key.Modifiers { func x11KeyStateToModifiers(s C.uint) key.Modifiers {
var m key.Modifiers var m key.Modifiers
if s&C.Mod1Mask != 0 {
m |= key.ModAlt
}
if s&C.Mod4Mask != 0 {
m |= key.ModSuper
}
if s&C.ControlMask != 0 { if s&C.ControlMask != 0 {
m |= key.ModCtrl m |= key.ModCtrl
} }
+8
View File
@@ -42,6 +42,8 @@ type Context struct {
var ( var (
_XKB_MOD_NAME_CTRL = []byte("Control\x00") _XKB_MOD_NAME_CTRL = []byte("Control\x00")
_XKB_MOD_NAME_SHIFT = []byte("Shift\x00") _XKB_MOD_NAME_SHIFT = []byte("Shift\x00")
_XKB_MOD_NAME_ALT = []byte("Mod1\x00")
_XKB_MOD_NAME_LOGO = []byte("Mod4\x00")
) )
func (x *Context) Destroy() { func (x *Context) Destroy() {
@@ -129,6 +131,12 @@ func (x *Context) DispatchKey(keyCode uint32) (events []event.Event) {
if C.xkb_state_mod_name_is_active(x.state, (*C.char)(unsafe.Pointer(&_XKB_MOD_NAME_SHIFT[0])), C.XKB_STATE_MODS_EFFECTIVE) == 1 { if C.xkb_state_mod_name_is_active(x.state, (*C.char)(unsafe.Pointer(&_XKB_MOD_NAME_SHIFT[0])), C.XKB_STATE_MODS_EFFECTIVE) == 1 {
cmd.Modifiers |= key.ModShift cmd.Modifiers |= key.ModShift
} }
if C.xkb_state_mod_name_is_active(x.state, (*C.char)(unsafe.Pointer(&_XKB_MOD_NAME_ALT[0])), C.XKB_STATE_MODS_EFFECTIVE) == 1 {
cmd.Modifiers |= key.ModAlt
}
if C.xkb_state_mod_name_is_active(x.state, (*C.char)(unsafe.Pointer(&_XKB_MOD_NAME_LOGO[0])), C.XKB_STATE_MODS_EFFECTIVE) == 1 {
cmd.Modifiers |= key.ModSuper
}
events = append(events, cmd) events = append(events, cmd)
} }
C.xkb_compose_state_feed(x.compState, sym) C.xkb_compose_state_feed(x.compState, sym)
+12
View File
@@ -64,6 +64,12 @@ const (
ModCommand ModCommand
// ModShift is the shift modifier key. // ModShift is the shift modifier key.
ModShift ModShift
// ModAlt is the alt modifier key, or the option
// key on Apple keyboards.
ModAlt
// ModSuper is the "logo" modifier key, often
// represented by a Windows logo.
ModSuper
) )
const ( const (
@@ -121,5 +127,11 @@ func (m Modifiers) String() string {
if m.Contain(ModShift) { if m.Contain(ModShift) {
strs = append(strs, "ModShift") strs = append(strs, "ModShift")
} }
if m.Contain(ModAlt) {
strs = append(strs, "ModAlt")
}
if m.Contain(ModSuper) {
strs = append(strs, "ModSuper")
}
return strings.Join(strs, "|") return strings.Join(strs, "|")
} }