Skip to content
This repository was archived by the owner on Jan 30, 2025. It is now read-only.

Commit f4014fe

Browse files
committed
Refactor keyboard and keyboard layout
1 parent 21cc7b9 commit f4014fe

File tree

2 files changed

+37
-25
lines changed

2 files changed

+37
-25
lines changed

common/keyboard.go

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ type Keyboard struct {
5050
modifiers int64 // like shift, alt, ctrl, ...
5151
pressedKeys map[int64]bool // tracks keys through down() and up()
5252
layoutName string // us by default
53+
layout keyboardlayout.KeyboardLayout
5354
}
5455

5556
// NewKeyboard returns a new keyboard with a "us" layout.
@@ -59,6 +60,7 @@ func NewKeyboard(ctx context.Context, session *Session) *Keyboard {
5960
session: session,
6061
pressedKeys: make(map[int64]bool),
6162
layoutName: "us",
63+
layout: keyboardlayout.GetKeyboardLayout("us"),
6264
}
6365
}
6466

@@ -113,8 +115,7 @@ func (k *Keyboard) Type(text string, opts goja.Value) {
113115

114116
func (k *Keyboard) down(key string) error {
115117
keyInput := keyboardlayout.KeyInput(key)
116-
layout := keyboardlayout.GetKeyboardLayout(k.layoutName)
117-
if _, ok := layout.ValidKeys[keyInput]; !ok {
118+
if _, ok := k.layout.ValidKeys[keyInput]; !ok {
118119
return fmt.Errorf("%q is not a valid key for layout %q", key, k.layoutName)
119120
}
120121

@@ -148,8 +149,7 @@ func (k *Keyboard) down(key string) error {
148149

149150
func (k *Keyboard) up(key string) error {
150151
keyInput := keyboardlayout.KeyInput(key)
151-
layout := keyboardlayout.GetKeyboardLayout(k.layoutName)
152-
if _, ok := layout.ValidKeys[keyInput]; !ok {
152+
if _, ok := k.layout.ValidKeys[keyInput]; !ok {
153153
return fmt.Errorf("'%s' is not a valid key for layout '%s'", key, k.layoutName)
154154
}
155155

@@ -178,28 +178,18 @@ func (k *Keyboard) insertText(text string) error {
178178
return nil
179179
}
180180

181-
func (k *Keyboard) keyDefinitionFromKey(keyString keyboardlayout.KeyInput) keyboardlayout.KeyDefinition {
182-
layout := keyboardlayout.GetKeyboardLayout(k.layoutName)
183-
srcKeyDef, ok := layout.Keys[keyString]
184-
// Find based on key value instead of code
181+
func (k *Keyboard) keyDefinitionFromKey(key keyboardlayout.KeyInput) keyboardlayout.KeyDefinition {
182+
shift := k.modifiers & ModifierKeyShift
183+
184+
// Find directly from the keyboard layout
185+
srcKeyDef, ok := k.layout.Keys[key]
186+
// Try to find based on key value instead of code
185187
if !ok {
186-
for key, def := range layout.Keys {
187-
if def.Key == string(keyString) {
188-
keyString, srcKeyDef = key, def
189-
ok = true // don't look for a shift key below
190-
break
191-
}
192-
}
188+
srcKeyDef, ok = k.layout.KeyDefinition(key)
193189
}
194-
// try to find with the shift key value
195-
shift := k.modifiers & ModifierKeyShift
190+
// Try to find with the shift key value
196191
if !ok {
197-
for key, def := range layout.Keys {
198-
if def.ShiftKey == string(keyString) {
199-
keyString, srcKeyDef = key, def
200-
break
201-
}
202-
}
192+
key, srcKeyDef = k.layout.ShiftKeyDefinition(key)
203193
shift = k.modifiers | ModifierKeyShift
204194
}
205195

@@ -214,8 +204,8 @@ func (k *Keyboard) keyDefinitionFromKey(keyString keyboardlayout.KeyInput) keybo
214204
if srcKeyDef.KeyCode != 0 {
215205
keyDef.KeyCode = srcKeyDef.KeyCode
216206
}
217-
if keyString != "" {
218-
keyDef.Code = string(keyString)
207+
if key != "" {
208+
keyDef.Code = string(key)
219209
}
220210
if srcKeyDef.Location != 0 {
221211
keyDef.Location = srcKeyDef.Location

keyboardlayout/common.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,28 @@ type KeyboardLayout struct {
4343
Keys map[KeyInput]KeyDefinition
4444
}
4545

46+
// KeyDefinition returns true with the key definition of a given key input.
47+
// It returns false and an empty key definition if it cannot find the key.
48+
func (kl KeyboardLayout) KeyDefinition(key KeyInput) (KeyDefinition, bool) {
49+
for _, d := range kl.Keys {
50+
if d.Key == string(key) {
51+
return d, true
52+
}
53+
}
54+
return KeyDefinition{}, false
55+
}
56+
57+
// ShiftKeyDefinition returns shift key definition of a given key input.
58+
// It an empty key definition if it cannot find the key.
59+
func (kl KeyboardLayout) ShiftKeyDefinition(key KeyInput) (KeyInput, KeyDefinition) {
60+
for k, d := range kl.Keys {
61+
if d.ShiftKey == string(key) {
62+
return k, d
63+
}
64+
}
65+
return key, KeyDefinition{}
66+
}
67+
4668
//nolint:gochecknoglobals
4769
var (
4870
kbdLayouts = make(map[string]KeyboardLayout)

0 commit comments

Comments
 (0)