# Key Code POC - Analysis of Issues

## 🐛 Problems Found

### Your Test Case
```
Input (Hebrew):  ישא „ם םו איןמל?
Expected:        What do ou think?
Got:             Hat „o ou think?
```

**Issues:**
1. Missing "W" at start of "What"
2. Lost capital letters  
3. Got capital "H" when it shouldn't be

---

## 🔍 Root Cause

### The Fundamental Problem

**CGEventTap can't reliably REPLACE characters!**

When we intercept an event:
1. System has already queued the original character
2. We create a new event with Hebrew char
3. Both events get processed
4. Results in double characters or missed characters

### Why It's Unreliable

```
User types: Shift+W
↓
CGEventTap intercepts
↓
We create event with "'" (Hebrew for W)
↓  
BUT: Original Shift+W event STILL processes
↓
Result: Gets both characters or neither!
```

---

## 💡 Better Solution: Hybrid Approach

### Don't Replace - Just Remember!

Instead of replacing characters in real-time, use key codes to **track capitalization**:

```
1. Monitor key events (listen-only, don't modify)
2. Remember which positions had Shift pressed
3. When hotkey pressed, use this info for conversion
4. Apply caps correctly
```

---

## 🎯 Implementation Strategy

### New Approach: Capitalization Tracker

```swift
class CapitalizationTracker {
    // Track the last N keystrokes with their shift state
    private var keystrokeHistory: [(char: String, wasShifted: Bool)] = []
    
    func recordKeystroke(char: String, wasShifted: Bool) {
        keystrokeHistory.append((char, wasShifted))
        
        // Keep only last 1000 characters
        if keystrokeHistory.count > 1000 {
            keystrokeHistory.removeFirst()
        }
    }
    
    func getCapitalizationPattern(for text: String) -> [Bool] {
        // Return array of which characters should be capitalized
        // Match the text against recent history
    }
}
```

### Usage in Conversion

```swift
// When hotkey pressed:
let text = getSelectedText()
let capPattern = CapitalizationTracker.shared.getCapitalizationPattern(for: text)
let converted = convert(text)

// Apply capitalization pattern
let result = applyCapitalization(converted, pattern: capPattern)
```

---

## 🚀 Recommendation

### For v1.5.x
**Don't try to replace characters in real-time** - it's too unreliable

Instead:
1. ✅ Keep current hotkey approach
2. ✅ Use smart capitalization (first letter, after punctuation)
3. ✅ Add capitalization tracker for better accuracy (optional)

### For v2.0
**If we want real-time conversion:**
- Need to use Input Method Kit (IME)
- Much more complex
- Requires app to be an input source
- But would work perfectly

---

## Decision: Disable KeyCodeMonitor POC

The current POC is too unreliable. Better to:
1. Remove the POC toggle from preferences
2. Keep the code for reference
3. Focus on improving the hotkey-based conversion
4. Plan proper IME implementation for v2.0

---

**Recommendation:** Should I disable the POC and focus on making the current approach better?

