lqt icon indicating copy to clipboard operation
lqt copied to clipboard

Handling KeyPress Event?

Open dmattp opened this issue 12 years ago • 2 comments

I'm not sure if this is a problem or it is just not clear to me how to do this. I'm trying to handle some keypresses to a QListWidget, but the problem I have is:

  1. Installing a virtual handler for keyPressEvent() “works”, but I can’t find a way to pass unhandled keypresses to or invoke the original (not-overridden) keyPressEvent function. That means I would have to duplicate the functionality of responding to all keys in lua. Is there a way to call QListWidget's original QListWidget::keyPressEvent() function from the widget:keyPressEvent() function that I add in lua?
  2. Alternatively, I can install an event filter and return false to indicate I didn’t handle the event which allows the original key press handler to maintain existing behavior- but the QEvent doesn’t seem to be modeled like other QT objects and I can’t figure out how to "cast" it to an actual keypress event, which seems to be the suggested method under C++. (eventFilter just gets a QEvent object, which under C++ has to be cast to the specific event type [QKeyEvent in this case] based on the event→type() function. ev:type() returns a string that says “KeyPress”, but how can I cast the event to an actual QKeyEvent in lua?

Any ideas? Is this possible already or are these functions not implemented yet?

dmattp avatar Jun 15 '12 15:06 dmattp

Sorry, looks like I posted too soon. The closed issue #9 says I can use "error(SUPER)" to call the base class. If there is an anwser to part 2, though, it would be appreciated.

dmattp avatar Jun 15 '12 15:06 dmattp

I am glad you found the workaround for your first question. Currently there is no casting for the userdata, but you can use the debug interface to change the metatable of the userdata:

local R = debug.getregistry()
local A = QApplication(1,{'Event'})
local W = QMainWindow()
function W:eventFilter(object, event)
    if event:type() == "KeyPress" then
        debug.setmetatable(event, R['QKeyEvent*']) -- "cast" it to QKeyEvent*
        local k = event:key() -- get the actual key
        if k<256 then k = string.char(k) end
        print('Key', k)
    end
    error(SUPER) -- this is the fallback mechanism to the original implementation
end
W:installEventFilter(W)
W:show()
A.exec()

mkottman avatar Jun 15 '12 19:06 mkottman