Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

the ALT key still gets stuck sometimes #60

Open
hannesdelbeke opened this issue Feb 22, 2023 · 7 comments
Open

the ALT key still gets stuck sometimes #60

hannesdelbeke opened this issue Feb 22, 2023 · 7 comments
Labels
bug Something isn't working

Comments

@hannesdelbeke
Copy link
Collaborator

a fix went in #32
but sometimes this bug still happens.
pressing ALT once fixes the bug.

@hannesdelbeke
Copy link
Collaborator Author

had the same happen with the CTRL key

@hannesdelbeke hannesdelbeke added bug Something isn't working enhancement New feature or request and removed enhancement New feature or request labels Jun 4, 2023
@hannesdelbeke
Copy link
Collaborator Author

hannesdelbeke commented Jun 6, 2023

when alt tabbing to another app. and back to blender, the alt key appears to not get stuck.
but when alt tabbing to the blender preference window and back to main blender, it gets always stuck.

100 % REPRO
you can test this:
open the blender prefernce window, and alt tab from there to the main blender window.
in the blender script editor, type R.
if alt is not stuck, it will type the letter R
if alt is stuck, it will ask you to reload

@hannesdelbeke
Copy link
Collaborator Author

hannesdelbeke commented Jun 6, 2023

❌ tried event simulate from blender

bpy.types.Window.event_simulate('LEFT_ALT', 'RELEASE')
# AttributeError: type object 'Window' has no attribute 'event_simulate'

needs to run on instance

new_window.event_simulate('LEFT_ALT', 'RELEASE')
# RuntimeError: Error: Not running with '--enable-event-simulate' enabled

@hannesdelbeke
Copy link
Collaborator Author

Yeah, there's a bunch of win32 side handoff that doesn't get handled properly.
I've done of a bit bunch for not-blender systems when linking GUI frameworks together (mostly wx and qt), always meant to wrap back around to bqt and see if any of the tricks worked.

@bob-white linked this https://github.com/qtproject/qt-solutions/tree/master/qtwinmigrate

Yes its C++, but most everything can be translated to python either through ctypes or pywin32.
But it helps cover some tricks on properly assigning child<>parent links, focus and message routing.

@hannesdelbeke
Copy link
Collaborator Author

current workaround: disable qt wrapping with BQT_DISABLE_WRAP set to 1

@hannesdelbeke
Copy link
Collaborator Author

hannesdelbeke commented Jul 17, 2023

some keys get stuck on alt tab or window focus change.

we successfullly force a release from stuck keys when changing focus from blender to another app and back. but not yet from blender to blender.

on QApp init, we connect _on_focus_object_changed to focusObjectChanged,
This always runs, even when swapping from blender to blender.

# win32 implementation
    def _on_focus_object_changed(self, focus_object: QObject):
        """
        Args:
            QObject focus_object: Object to track focus change
        """
        if focus_object is self.blender_widget:
            ctypes.windll.user32.SetFocus(self._hwnd)
            bqt.focus._detect_keyboard(self._hwnd)

_on_focus_object_changed triggers _detect_keyboard():

@staticmethod
def _detect_keyboard():
    """
    force a release of 'stuck' keys
    """

    # key codes from https://itecnote.com/tecnote/python-simulate-keydown/
    keycodes = [
        ("_ALT", 0x12),
        ("_CTRL", 0x11),
        ("_SHIFT", 0x10),
        ("VK_LWIN", 0x5B),
        ("VK_RWIN", 0x5C),
        ("OSKEY", 0x5B),  # dupe oskey, blender names it this
    ]

    # print("event.type", event.type, type(event.type))
    for name, code in keycodes:

        # todo this bug fix is not perfect yet, blender works better without this atm
        # # if the first key pressed is one of the following,
        # # don't simulate a key release, since it causes this bug:
        # # the first keypress on re-focus blender will be ignored, e.g. ctrl + v will just be v
        # if name in event.type:
        #     print("skipping:", name)
        #     continue

        # safely release all other keys that might be stuck down
        ctypes.windll.user32.keybd_event(code, 0, 2, 0)  # release key
        # print("released key", name, code)

    # todo, fix: blender occasionally still frozen input, despite having run the above code
    # but when we click the mouse, it starts working again
    # simulate a right mouse click did not work ...
    # ctypes.windll.user32.mouse_event(0x0008, 0, 0, 0, 0)  # right mouse click

@hannesdelbeke
Copy link
Collaborator Author

hannesdelbeke commented Jul 17, 2023

  • _detect_keyboard triggers when alt tab from blender preferences, to blender main,
    yet the alt key still appears to be stuck
  • it doesn't trigger on alt tab to blender preferences.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant