PyQt eventFilter KeyPress

2,830 views
Skip to first unread message

luiz elias

unread,
Feb 12, 2013, 5:35:20 AM2/12/13
to python_in...@googlegroups.com
Hi all,

I'm trying get a eventFilter to work and get the keyboard press event,
it works fine for me with mouse events but I can't get the keyboard events to work,
i guess I'm missing something,
this is the far I got:

import sip
import maya.OpenMayaUI as OpenMayaUI
from PyQt4 import QtGui, QtCore

class MouseEventFilter(QtCore.QObject):
def eventFilter(self, obj, event):
if event.type() == QtCore.QEvent.KeyPress:
print "working..."
return True
return False

view = OpenMayaUI.M3dView.active3dView()
widget = sip.wrapinstance(long(view.widget()), QtCore.QObject)
eventFilter = MouseEventFilter(widget)

widget.installEventFilter(eventFilter)

#widget.removeEventFilter(eventFilter)


problem is that the QtCore.QEvent.KeyPress never happends =/
any ideas why??

thnaks!

Luiz

Jiet Shern Neo

unread,
Feb 12, 2013, 10:48:26 AM2/12/13
to python_in...@googlegroups.com
I recently was working on some keypress event too (in c++ though but I think the issues might be the same). One thing I realize is that keypress event only appear when the widget is in focus. I have to explicitly set the widget in focus when my mouse enter the widget (I am using the keypress in combination with my mouse to activate a tool) You might want to check if the loss of focus of your widget might be the issue.



Luiz

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To post to this group, send email to python_in...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



luiz elias

unread,
Feb 12, 2013, 12:17:41 PM2/12/13
to python_in...@googlegroups.com
thanks for reply!
do you mind to show some code?

I've tried this, but still not working..


import sip
import maya.OpenMayaUI as OpenMayaUI
from PyQt4 import QtGui, QtCore
import PyQt4

class MouseEventFilter(QtGui.QWidget):
    def __init__(self, parent):
        QtGui.QWidget.__init__(self, parent)
        self.setFocusPolicy(QtCore.Qt.StrongFocus)

   
    def eventFilter(self, obj, event):
        if event.type() == QtCore.QEvent.KeyPress:
            print "key press"
            return False

        return False
       
view = OpenMayaUI.M3dView.active3dView()
widget = sip.wrapinstance(long(view.widget()), QtCore.QObject)
eventFilter = MouseEventFilter(widget)

widget.installEventFilter(eventFilter)
#widget.removeEventFilter(eventFilter)


thank you!

luiz elias

unread,
Feb 13, 2013, 5:35:08 AM2/13/13
to python_in...@googlegroups.com
it works fine if I install the eventFilter on the main window, but I can't find a way to set keyboard focus for the QGLWidget.

thanks!


Justin Israel

unread,
Feb 13, 2013, 1:56:35 PM2/13/13
to python_in...@googlegroups.com
I got it working after a click occurs in the viewport, but not when you just try and call setFocus() on the widget.


On Feb 13, 2013, at 11:35 PM, luiz elias wrote:

it works fine if I install the eventFilter on the main window, but I can't find a way to set keyboard focus for the QGLWidget.

thanks!



Justin Israel

unread,
Feb 13, 2013, 2:01:27 PM2/13/13
to python_in...@googlegroups.com
Sent too quickly...
I meant to say that just calling setFocus() still seems to require a click before it really grabs the keyboard:


import sip
import maya.OpenMayaUI as OpenMayaUI
from PyQt4 import QtGui, QtCore

class MouseEventFilter(QtCore.QObject):
    def eventFilter(self, obj, event):
        typ = event.type()
        if typ == event.Enter or typ == event.MouseButtonPress:
            print "mouse enter"
            obj.setFocus()
        elif typ == event.Leave:
            print "mouse leave"
            obj.clearFocus()
        elif typ == event.KeyPress:
            print "working..."
            return True
        return False

view = OpenMayaUI.M3dView.active3dView()
widget = sip.wrapinstance(long(view.widget()), QtCore.QObject)
eventFilter = MouseEventFilter()
widget.installEventFilter(eventFilter)

# remove
widget.removeEventFilter(eventFilter)
eventFilter.deleteLater()

Ravi Jagannadhan

unread,
Feb 13, 2013, 2:04:06 PM2/13/13
to python_in...@googlegroups.com
If this is on Linux, what is your mouse focus policy in the OS? Is it click to focus?
--
Where we have strong emotions, we're liable to fool ourselves - Carl Sagan

Justin Israel

unread,
Feb 13, 2013, 2:16:06 PM2/13/13
to python_in...@googlegroups.com
Not sure if that question was directed at me or Luiz, but I am using click to focus for my linux setting. But I would assume an explicit setFocus() call would do it. It is required for me to even see a reaction after I then click and start pressing keys, but I also tried to grabKeyboard which made no difference in terms of requiring the click.

Ravi Jagannadhan

unread,
Feb 13, 2013, 2:18:23 PM2/13/13
to python_in...@googlegroups.com
Sorry, I should've clarified, I was asking you :) I wonder if the OS setting overrides the behaviour, which is why I'd asked.

Justin Israel

unread,
Feb 13, 2013, 2:28:59 PM2/13/13
to python_in...@googlegroups.com

Gah. I just realized that when I had tested this the other day, it was on my Mac. So I can't speak for Linux but I can test that theory when I get to work.
Strangely when I set focus for normal widgets like a line edit, they grab the keyboard. So maybe its a bit different for Maya based Qt widgets.

luiz elias

unread,
Feb 14, 2013, 8:34:49 AM2/14/13
to python_in...@googlegroups.com
it works fine if I install the filter on the main window, so I end up creating 2 filter one for mouse click on the main window and another for mouse pos on the GLWidget..
this is for use in conjunction with a MPxContext by the way = )
thanks!

luiz
Reply all
Reply to author
Forward
0 new messages