[Minor Feature] Engine-level modifier key state tracking #160

Closed
opened 2025-12-28 20:23:11 +00:00 by john · 0 comments
Owner

Summary

Currently, detecting modifier key combinations (like Shift+Tab, Ctrl+S) requires Python code to manually track modifier state by processing press/release events. This is error-prone and duplicates effort across projects.

Proposed Solution

Add engine-level tracking of modifier key state, exposed to Python via:

# Option A: Module-level functions
mcrfpy.is_key_pressed("LShift")  # or "RShift", "LCtrl", etc.

# Option B: Keyboard state object
mcrfpy.keyboard.shift  # bool
mcrfpy.keyboard.ctrl   # bool
mcrfpy.keyboard.alt    # bool

# Option C: Pass modifiers to key handler
def on_key(key: str, action: str, modifiers: dict):
    if key == "Tab" and modifiers["shift"]:
        focus_previous()

Benefits

  1. Accuracy: Engine tracks state even if Python misses an event
  2. Simplicity: No boilerplate modifier tracking in every project
  3. Future expansion: Foundation for global hotkey system (Ctrl+S = save, etc.)

Implementation Notes

SFML provides sf::Keyboard::isKeyPressed() which can query modifier state at any time. This could be exposed directly or used to populate a modifiers dict passed to handlers.

Workaround

Until implemented, Python code can track modifiers manually:

class ModifierTracker:
    def __init__(self):
        self.shift = False
        self.ctrl = False
        self.alt = False
    
    def update(self, key: str, action: str):
        if key in ("LShift", "RShift"):
            self.shift = (action == "start")
        elif key in ("LControl", "RControl"):
            self.ctrl = (action == "start")
        elif key in ("LAlt", "RAlt"):
            self.alt = (action == "start")
  • Discovered while implementing #143 (Focus System Demo)
  • Could expand into global hotkey registration system
## Summary Currently, detecting modifier key combinations (like Shift+Tab, Ctrl+S) requires Python code to manually track modifier state by processing press/release events. This is error-prone and duplicates effort across projects. ## Proposed Solution Add engine-level tracking of modifier key state, exposed to Python via: ```python # Option A: Module-level functions mcrfpy.is_key_pressed("LShift") # or "RShift", "LCtrl", etc. # Option B: Keyboard state object mcrfpy.keyboard.shift # bool mcrfpy.keyboard.ctrl # bool mcrfpy.keyboard.alt # bool # Option C: Pass modifiers to key handler def on_key(key: str, action: str, modifiers: dict): if key == "Tab" and modifiers["shift"]: focus_previous() ``` ## Benefits 1. **Accuracy**: Engine tracks state even if Python misses an event 2. **Simplicity**: No boilerplate modifier tracking in every project 3. **Future expansion**: Foundation for global hotkey system (Ctrl+S = save, etc.) ## Implementation Notes SFML provides `sf::Keyboard::isKeyPressed()` which can query modifier state at any time. This could be exposed directly or used to populate a modifiers dict passed to handlers. ## Workaround Until implemented, Python code can track modifiers manually: ```python class ModifierTracker: def __init__(self): self.shift = False self.ctrl = False self.alt = False def update(self, key: str, action: str): if key in ("LShift", "RShift"): self.shift = (action == "start") elif key in ("LControl", "RControl"): self.ctrl = (action == "start") elif key in ("LAlt", "RAlt"): self.alt = (action == "start") ``` ## Related - Discovered while implementing #143 (Focus System Demo) - Could expand into global hotkey registration system
john closed this issue 2025-12-29 21:24:38 +00:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
john/McRogueFace#160
No description provided.