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

Handling KeyPress Event? #47

Open
dmattp opened this issue Jun 15, 2012 · 2 comments
Open

Handling KeyPress Event? #47

dmattp opened this issue Jun 15, 2012 · 2 comments

Comments

@dmattp
Copy link

dmattp commented Jun 15, 2012

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
Copy link
Author

dmattp commented Jun 15, 2012

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.

@mkottman
Copy link
Owner

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()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants