Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ All notable changes to OpenWritr will be documented in this file.
Each release's notes on GitHub are generated from its section here, and the
broker publication handoff fails when the section for a tag is missing or empty.

## [1.7.5] — 2026-10-29

### Fixed
- Typing into Windows App (**Type Text in Remote Desktop Apps**) now produces shifted characters correctly. OpenWritr's simulated Shift key did not mark which Shift key was pressed, so the remote PC could receive the unshifted key instead: on a German layout `?` arrived as `ß`. Shift is now sent as the left Shift key, like a real key press

## [1.7.4] — 2026-10-13

### Fixed
Expand Down
4 changes: 2 additions & 2 deletions Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
<key>CFBundleIdentifier</key>
<string>com.openwritr.app</string>
<key>CFBundleVersion</key>
<string>1.7.4</string>
<string>1.7.5</string>
<key>CFBundleShortVersionString</key>
<string>1.7.4</string>
<string>1.7.5</string>
<key>NSHumanReadableCopyright</key>
<string>Copyright © 2026 Torsten Mahr. Released under the MIT License.</string>
<key>OpenWritrDescription</key>
Expand Down
15 changes: 10 additions & 5 deletions Sources/OpenWritr/TextTyping.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Carbon.HIToolbox
import Cocoa
import CoreGraphics
import IOKit.hidsystem
import os.log

private let typingLog = Logger(subsystem: "com.openwritr.app", category: "TextTyping")
Expand Down Expand Up @@ -208,6 +209,10 @@ enum TypingDelivery {

struct SystemTextTyper: TextTyping {
private static let shiftKeyCode: CGKeyCode = 0x38
// Remote clients can track left/right modifiers using the device-dependent flags.
private static let shiftFlags = CGEventFlags.maskShift.union(
CGEventFlags(rawValue: UInt64(NX_DEVICELSHIFTKEYMASK))
)
/// Remote desktop clients can drop key events that arrive faster than they are forwarded.
private nonisolated static let eventSpacing: useconds_t = 3_000
private nonisolated static let queue = DispatchQueue(label: "com.openwritr.text-typing")
Expand Down Expand Up @@ -255,11 +260,11 @@ struct SystemTextTyper: TextTyping {
}
}

private static func eventGroups(for strokes: [KeyStroke], source: CGEventSource) -> [[CGEvent]]? {
static func eventGroups(for strokes: [KeyStroke], source: CGEventSource) -> [[CGEvent]]? {
var groups: [[CGEvent]] = []
for stroke in strokes {
var events: [CGEvent] = []
let flags: CGEventFlags = stroke.shift ? .maskShift : []
let flags: CGEventFlags = stroke.shift ? shiftFlags : []
if stroke.shift {
guard let shiftDown = modifierEvent(source: source, down: true) else { return nil }
events.append(shiftDown)
Expand All @@ -283,10 +288,10 @@ struct SystemTextTyper: TextTyping {
/// Shift is sent as a real modifier key event, because a remote desktop client tracks modifier
/// changes rather than trusting the flags of a key event.
private static func modifierEvent(source: CGEventSource, down: Bool) -> CGEvent? {
guard let event = CGEvent(source: source) else { return nil }
guard let event = CGEvent(keyboardEventSource: source, virtualKey: shiftKeyCode, keyDown: down)
else { return nil }
event.type = .flagsChanged
event.setIntegerValueField(.keyboardEventKeycode, value: Int64(shiftKeyCode))
event.flags = down ? .maskShift : []
event.flags = down ? shiftFlags : []
return event
}
}
35 changes: 35 additions & 0 deletions Tests/OpenWritrTests/TextTypingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,41 @@ struct TextTypingPlanTests {
}
}

@MainActor
@Suite("Remote keyboard events")
struct RemoteKeyboardEventTests {
@Test func germanQuestionMarkAndCapitalsCarryLeftShiftUntilKeyRelease() throws {
let layout: [Character: KeyStroke] = [
"?": KeyStroke(keyCode: 0x1B, shift: true),
"ß": KeyStroke(keyCode: 0x1B, shift: false),
"A": KeyStroke(keyCode: 0, shift: true),
"a": KeyStroke(keyCode: 0, shift: false),
]
let plan = TextTypingPlanner.plan(for: "?ßAa", layout: layout)
let source = try #require(CGEventSource(stateID: .privateState))
let groups = try #require(SystemTextTyper.eventGroups(for: plan.strokes, source: source))

#expect(groups.count == 4)
for index in [0, 2] {
let events = groups[index]
#expect(events.map(\.type) == [.flagsChanged, .keyDown, .keyUp, .flagsChanged])
#expect(events.map { $0.getIntegerValueField(.keyboardEventKeycode) } == [
0x38, Int64(plan.strokes[index].keyCode), Int64(plan.strokes[index].keyCode), 0x38,
])
for event in events.prefix(3) {
#expect(event.flags.contains(.maskShift))
#expect(event.flags.rawValue & 0x00000002 != 0, "The left Shift flag must reach the remote client")
#expect(event.flags.rawValue & 0x00000004 == 0, "Right Shift is not pressed")
}
#expect(events[3].flags.isEmpty, "Release Shift after the character's key-up")
}
for index in [1, 3] {
#expect(groups[index].map(\.type) == [.keyDown, .keyUp])
#expect(groups[index].allSatisfy { $0.flags.isEmpty })
}
}
}

@MainActor
@Suite("System keyboard layout")
struct SystemKeyboardLayoutTests {
Expand Down
Loading