Skip to content

Commit

Permalink
use virtual keycodes on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
Nimaoth committed Jan 7, 2024
1 parent dc1a01e commit 54ba534
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 15 deletions.
2 changes: 1 addition & 1 deletion absytree.nimble
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ requires "glob#64f71af" # "glob >= 0.11.2" # the newest version of glob doesn't
requires "patty >= 0.3.5"
requires "nimclipboard >= 0.1.2"
requires "https://github.com/Nimaoth/ws >= 0.5.0"
requires "https://github.com/Nimaoth/windy >= 0.0.1"
requires "https://github.com/Nimaoth/windy >= 0.0.2"
requires "https://github.com/Nimaoth/wasm3 >= 0.1.13"
requires "https://github.com/Nimaoth/lrucache.nim >= 1.1.4"
requires "https://github.com/Nimaoth/boxy >= 0.4.2"
Expand Down
3 changes: 1 addition & 2 deletions src/platform/browser_platform.nim
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,9 @@ method init*(self: BrowserPlatform) =

let ke = e.KeyboardEvent
let modifiers = ke.getModifiers
# debugf"keyevent {ke.key}, {ke.code}, {ke.keyCode}"

var input = toInput(ke.key, ke.code, ke.keyCode)
# debugf"{inputToString(input)}, {modifiers}"
# debugf"keyevent k: {ke.key}, c: {ke.code}, kc: {ke.keyCode}, input: {inputToString(input, modifiers)}"
if not self.builder.handleKeyPressed(input, modifiers):
self.onKeyPress.invoke (input, modifiers)
)
Expand Down
35 changes: 24 additions & 11 deletions src/platform/gui_platform.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import input, monitors, lrucache, theme

export platform

logCategory "gui-platform"

type
GuiPlatform* = ref object of Platform
window: Window
Expand Down Expand Up @@ -38,7 +40,7 @@ type

cachedImages: LruCache[string, string]

lastEvent: Option[(int64, Modifiers)]
lastEvent: Option[(int64, Modifiers, Button)]

drawnNodes: seq[UINode]

Expand All @@ -55,6 +57,13 @@ method init*(self: GuiPlatform) =
self.window.runeInputEnabled = true
self.supportsThinCursor = true

# Use virtual key codes so that we can take into account the keyboard language
# and the behaviour is more consistent with the browser/terminal.
# todo: is this necessary on linux?
when defined(windows):
log lvlInfo, "Using virtual key codes instead of scan codes"
self.window.useVirtualKeyCodes = true

self.builder = newNodeBuilder()
self.builder.useInvalidation = true

Expand Down Expand Up @@ -124,7 +133,7 @@ method init*(self: GuiPlatform) =

# debugf"rune {rune.int} '{rune}' {inputToString(rune.toInput, self.currentModifiers)}"
if self.lastEvent.isSome:
self.lastEvent = (int64, Modifiers).none
self.lastEvent = (int64, Modifiers, Button).none

self.onRune.invoke (rune.toInput, self.currentModifiers)

Expand All @@ -151,12 +160,13 @@ method init*(self: GuiPlatform) =
self.window.onButtonPress = proc(button: Button) =
inc self.eventCounter

# debugf"button {button} {inputToString(button.toInput, self.currentModifiers)}"

if self.lastEvent.getSome(event):
# debugf"button last event k: {event[2]}, input: {inputToString(event[0], event[1])}"
if not self.builder.handleKeyPressed(event[0], event[1]):
self.onKeyPress.invoke event
self.lastEvent = (int64, Modifiers).none
self.onKeyPress.invoke (event[0], event[1])
self.lastEvent = (int64, Modifiers, Button).none

# debugf"button k: {button}, input: {inputToString(button.toInput, self.currentModifiers)}"

case button
of MouseLeft, MouseRight, MouseMiddle, MouseButton4, MouseButton5, DoubleClick, TripleClick, QuadrupleClick:
Expand All @@ -168,15 +178,17 @@ method init*(self: GuiPlatform) =
of KeyLeftAlt, KeyRightAlt: self.currentModifiers = self.currentModifiers + {Alt}
# of KeyLeftSuper, KeyRightSuper: currentModifiers = currentModifiers + {Super}
else:
self.lastEvent = (button.toInput, self.currentModifiers).some
# debugf"last event k: {button}, input: {inputToString(button.toInput, self.currentModifiers)}"
self.lastEvent = (button.toInput, self.currentModifiers, button).some

self.window.onButtonRelease = proc(button: Button) =
inc self.eventCounter

if self.lastEvent.getSome(event):
# debugf"button release last event k: {event[2]}, input: {inputToString(event[0], event[1])}"
if not self.builder.handleKeyPressed(event[0], event[1]):
self.onKeyPress.invoke event
self.lastEvent = (int64, Modifiers).none
self.onKeyPress.invoke (event[0], event[1])
self.lastEvent = (int64, Modifiers, Button).none

case button
of MouseLeft, MouseRight, MouseMiddle, MouseButton4, MouseButton5, DoubleClick, TripleClick, QuadrupleClick:
Expand Down Expand Up @@ -266,9 +278,10 @@ method processEvents*(self: GuiPlatform): int =
pollEvents()

if self.lastEvent.getSome(event):
# debugf"process last event k: {event[2]}, input: {inputToString(event[0], event[1])}"
if not self.builder.handleKeyPressed(event[0], event[1]):
self.onKeyPress.invoke event
self.lastEvent = (int64, Modifiers).none
self.onKeyPress.invoke (event[0], event[1])
self.lastEvent = (int64, Modifiers, Button).none

if self.window.closeRequested:
inc self.eventCounter
Expand Down
3 changes: 2 additions & 1 deletion src/platform/terminal_platform.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import platform

export platform

logCategory "terminal"
logCategory "terminal-platform"

type
TerminalPlatform* = ref object of Platform
Expand Down Expand Up @@ -262,6 +262,7 @@ method processEvents*(self: TerminalPlatform): int =
else:
var modifiers: Modifiers = {}
let button = key.toInput(modifiers)
# debugf"key press k: {key}, input: {inputToString(button, modifiers)}"
if not self.builder.handleKeyPressed(button, modifiers):
self.onKeyPress.invoke (button, modifiers)

Expand Down

0 comments on commit 54ba534

Please sign in to comment.