Skip to content

Commit bd8ec25

Browse files
committed
Basic text input on nosdl macos
1 parent 02bd90d commit bd8ec25

File tree

2 files changed

+64
-34
lines changed

2 files changed

+64
-34
lines changed

nimx/keyboard.nim

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,18 +93,18 @@ type VirtualKey* {.pure.} = enum
9393
F10
9494
NumLock
9595
ScrollLock
96+
Keypad0
97+
Keypad1
98+
Keypad2
99+
Keypad3
100+
Keypad4
101+
Keypad5
102+
Keypad6
96103
Keypad7
97104
Keypad8
98105
Keypad9
99106
KeypadMinus
100-
Keypad4
101-
Keypad5
102-
Keypad6
103107
KeypadPlus
104-
Keypad1
105-
Keypad2
106-
Keypad3
107-
Keypad0
108108
KeypadPeriod
109109
NonUSBackSlash
110110
F11
@@ -202,11 +202,23 @@ proc toAscii*(vk: VirtualKey): char =
202202
char(ord(vk) - ord(VirtualKey.A) + ord('a'))
203203
of VirtualKey.Zero .. VirtualKey.Nine:
204204
char(ord(vk) - ord(VirtualKey.Zero) + ord('0'))
205+
of VirtualKey.Keypad0 .. VirtualKey.Keypad9:
206+
char(ord(vk) - ord(VirtualKey.Keypad0) + ord('0'))
205207
of VirtualKey.Space: ' '
206208
of VirtualKey.RightBracket: ']'
207209
of VirtualKey.LeftBracket: '['
208210
of VirtualKey.Minus, VirtualKey.KeypadMinus: '-'
209-
of VirtualKey.Equals: '='
211+
of VirtualKey.Equals, VirtualKey.KeypadEquals: '='
212+
of VirtualKey.KeypadPlus: '+'
213+
of VirtualKey.KeypadMultiply: '*'
214+
of VirtualKey.KeypadDivide: '/'
215+
of VirtualKey.Semicolon: ';'
216+
of VirtualKey.Apostrophe: '\''
217+
of VirtualKey.Backtick: '`'
218+
of VirtualKey.Period, VirtualKey.KeypadComma: '.'
219+
of VirtualKey.Comma: ','
220+
of VirtualKey.Slash: '/'
221+
of VirtualKey.BackSlash: '\\'
210222
else:
211223
char(0)
212224

nimx/private/windows/appkit_window.nim

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ type
3434
nativeView: NimxView
3535
renderingContext: GraphicsContext
3636
displayLink: CVDisplayLink
37+
textInputActive: bool
3738

3839
const
3940
AppDelegateClass = "NimxAppDelegate"
@@ -458,13 +459,13 @@ proc nextEvent(evt: var sdl2.Event) =
458459
break
459460
460461
animateAndDraw()
462+
]#
461463

462-
method startTextInput*(w: EmscriptenWindow, r: Rect) =
463-
startTextInput()
464+
method startTextInput*(w: AppkitWindow, r: Rect) =
465+
w.textInputActive = true
464466

465-
method stopTextInput*(w: EmscriptenWindow) =
466-
stopTextInput()
467-
]#
467+
method stopTextInput*(w: AppkitWindow) =
468+
w.textInputActive = false
468469

469470
proc runUntilQuit(cb: proc()) =
470471
let pool = NSAutoreleasePool.alloc().init()
@@ -519,6 +520,8 @@ proc appkitModFlags(f: uint): ModifiersSet =
519520
result.incl(LeftControl)
520521

521522
proc eventWithNSEvent(w: AppkitWindow, e: NSEvent): Event =
523+
let mods = appkitModFlags(cast[uint](e.modifierFlags))
524+
522525
case e.kind
523526
of NSLeftMouseDown:
524527
result = newMouseButtonEvent(w.pointFromNSEvent(e), VirtualKey.MouseButtonPrimary, bsDown)
@@ -532,14 +535,25 @@ proc eventWithNSEvent(w: AppkitWindow, e: NSEvent): Event =
532535
result = newMouseButtonEvent(w.pointFromNSEvent(e), VirtualKey.MouseButtonSecondary, bsUp)
533536
of NSRightMouseDragged:
534537
result = newMouseButtonEvent(w.pointFromNSEvent(e), VirtualKey.MouseButtonSecondary, bsUnknown)
538+
of NSScrollWheel:
539+
result = newEvent(etScroll, w.pointFromNSEvent(e))
540+
result.offset.x = e.scrollingDeltaX
541+
result.offset.y = e.scrollingDeltaY
535542
of NSKeyDown:
536-
result = newKeyboardEvent(virtualKeyFromNative(e.keyCode.int), bsDown, e.isARepeat)
543+
let vk = virtualKeyFromNative(e.keyCode.int)
544+
if w.textInputActive and
545+
vk.toAscii != char(0) and
546+
not mods.anyCtrl and not mods.anyGui:
547+
result = newEvent(etTextInput)
548+
result.text = $e.characters
549+
else:
550+
result = newKeyboardEvent(vk, bsDown, e.isARepeat)
537551
of NSKeyUp:
538552
result = newKeyboardEvent(virtualKeyFromNative(e.keyCode.int), bsUp, e.isARepeat)
539553
else:
540554
discard
541555

542-
result.modifiers = appkitModFlags(cast[uint](e.modifierFlags))
556+
result.modifiers = mods
543557
result.window = w
544558

545559
proc getNimxWindow(v: NimxView): AppkitWindow =
@@ -569,6 +583,14 @@ proc reshape(self: NimxView, sel: SEL) =
569583
proc onDisplayLinkMainThread(self: NimxView, s: SEL, r: NSObject) =
570584
self.setNeedsDisplay(true)
571585

586+
proc handleEvent(self: NimxView, cmd: SEL, e: NSEvent) =
587+
let w = self.getNimxWindow()
588+
var evt = w.eventWithNSEvent(e)
589+
if not mainApplication().handleEvent(evt):
590+
var s = ObjcSuper(receiver: self, superClass: getSuperclass(viewClass()))
591+
let superImp = cast[proc(s: var ObjcSuper, c: SEL, e: NSEvent) {.cdecl.}](objc_msgSendSuper)
592+
superImp(s, cmd, e)
593+
572594
proc createViewClass(): ObjcClass =
573595
result = allocateClassPair(getClass("NSOpenGLView"), "NimxView", 0)
574596

@@ -577,16 +599,23 @@ proc createViewClass(): ObjcClass =
577599
discard addMethod(result, sel_registerName("reshape"), reshape)
578600
discard addMethod(result, sel_registerName("onDisplayLinkMainThread:"), onDisplayLinkMainThread)
579601

580-
registerClassPair(result)
581-
602+
template ev(e: cstring) =
603+
discard addMethod(result, sel_registerName(e), handleEvent)
604+
ev("keyDown:")
605+
ev("keyUp:")
606+
ev("mouseDown:")
607+
ev("mouseUp:")
608+
ev("mouseDragged:")
609+
ev("rightMouseDown:")
610+
ev("rightMouseUp:")
611+
ev("rightMouseDragged:")
612+
ev("otherMouseDown:")
613+
ev("otherMouseUp:")
614+
ev("otherMouseDragged:")
615+
ev("scrollWheel:")
582616

583-
# - (void)keyDown: (NSEvent*) e {
584-
# [super keyDown: e];
585-
# }
617+
registerClassPair(result)
586618

587-
# - (void)keyUp: (NSEvent*) e {
588-
# [super keyUp: e];
589-
# }
590619

591620
# - (void)insertText:(id)string replacementRange:(NSRange)replacementRange {
592621
# NSLog(@"text: %@", string);
@@ -637,17 +666,6 @@ proc createViewClass(): ObjcClass =
637666
# """.}
638667

639668

640-
proc sendEvent(self: NimxWindow, cmd: SEL, e: NSEvent) {.cdecl.} =
641-
var s = ObjcSuper(receiver: self, superClass: getSuperclass(windowClass()))
642-
let superImp = cast[proc(s: var ObjcSuper, c: SEL, e: NSEvent) {.cdecl.}](objc_msgSendSuper)
643-
superImp(s, cmd, e)
644-
645-
let w = self.getNimxWindow()
646-
var evt = w.eventWithNSEvent(e)
647-
discard mainApplication().handleEvent(evt)
648-
649669
proc createWindowClass(): ObjcClass =
650670
result = allocateClassPair(getClass("NSWindow"), "NimxWindow", 0)
651-
discard addMethod(result, sel_registerName("sendEvent:"), sendEvent)
652-
653671
registerClassPair(result)

0 commit comments

Comments
 (0)