Skip to content

Add glfwSetInputMethodCursorPos and improve IME behavior on macOS and Win32#2823

Open
chentong-net wants to merge 1 commit into
glfw:masterfrom
chentong-net:ime-input-method
Open

Add glfwSetInputMethodCursorPos and improve IME behavior on macOS and Win32#2823
chentong-net wants to merge 1 commit into
glfw:masterfrom
chentong-net:ime-input-method

Conversation

@chentong-net
Copy link
Copy Markdown

Summary

This PR improves IME integration in GLFW for desktop text input workflows.

What’s included

  1. New API: glfwSetInputMethodCursorPos(GLFWwindow* window, double xpos, double ypos)
    • Lets applications provide the current text cursor anchor position (content-area coordinates).
    • ntended for IME candidate/composition UI positioning.
  2. macOS (Cocoa) improvements
    • firstRectForCharacterRange now uses the provided input method cursor position and converts it to screen coordinates.
    • During IME composition, editing keys (Backspace/Delete/Enter/Arrow/Home/End/Escape) are not forwarded to app key handlers, preventing accidental deletion of already committed text.
    • Composition state is tracked from setMarkedText / unmarkText / insertText.
  3. Win32 improvements
    • glfwSetInputMethodCursorPos updates IME candidate/composition windows via:
      • ImmSetCandidateWindow
      • ImmSetCompositionWindow
    • Position is converted with window content scale so placement remains correct under DPI scaling.
  4. Build update
    • Links imm32 when building Win32 backend.

Motivation

In real-world IME usage:

  • Candidate windows should appear near the text caret, not at a fallback corner.
  • During preedit/composition, Backspace should edit the preedit text only, not delete committed text in the application input field.
    These issues are especially visible with CJK input methods.

API Notes

  • The new function stores an IME anchor position per window.
  • It is safe to call every frame / whenever caret position changes.
  • Non-implemented platforms will still compile and keep existing behavior.

Files changed

  • glfw3.h
  • internal.h
  • input.c
  • cocoa_window.m
  • CMakeLists.txt

Validation

Tested locally with:

  • macOS: candidate window follows caret; Backspace during composition no longer deletes committed app text.
  • Win32 code path implemented and build linkage updated (imm32).

@dougbinks dougbinks added enhancement Feature suggestions and PRs input Keyboard, joystick or mouse labels Mar 1, 2026
@chentong-net
Copy link
Copy Markdown
Author

Hi @elmindreda @dougbinks , just a friendly reminder. Could you please take a look at this PR when you have a moment? If everything looks good, I'd appreciate it if it could be merged. If any changes are needed, I'm happy to make them. If it's not suitable, I can close it. Thanks for your time!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Feature suggestions and PRs input Keyboard, joystick or mouse

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants