techartorg / bqt

A Blender add-on to support & manage Qt Widgets in Blender (PySide2)

Home Page:https://github.com/techartorg/bqt/wiki

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

the ALT key still gets stuck sometimes

hannesdelbeke opened this issue · comments

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

had the same happen with the CTRL key

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

❌ 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

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.

current workaround: disable qt wrapping with BQT_DISABLE_WRAP set to 1

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
  • _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.