Yes, this is possible.

from PySide import QtGui, QtCore
class MyEventHandler(QtCore.QObject):
  def eventFilter(self, target, event):
    if event.type() == event.KeyPress:
      print("That's a keypress")
    return super(MyEventHandler, self).eventFilter(target, event)

handler = MyEventHandler()

app = QtGui.QApplication.instance()
app.installEventFilter(handler)# app.removeEventFilter(handler)

That would normally be fine, *however* if there is one object where you
never want a (Python) event filter, it would be the QApplication or Maya’s
MainWindow. Because what happens is that *every single event* in Maya which
was previously highly optimised C++ now has to pass through your Python
function. Which means that even if your function does nothing but pass the
event through, it still has a *significant* effect on performance. For
starters, you should notice that with this filter installed, navigating in
the viewport suddenly has a slight delay. As if the click caused a minor
lag before eventually allowing you to dolly the camera.

To put a picture in your head, here’s why.

Information Super Highway

--------------\                 /--------------->
-------------- \               /---------------->
---------------->-------------/----------------->
-------------- /              \----------------->
--------------/                \---------------->
      C++           Python             C++

Instead, what you should consider, if you are able, is installing an event
filter *not* in QApplication or Maya’s main window, but in *your widget*.

class MyWidget(QtGui.QDialog):
  def __init__(self, parent=None):
    super(MyWidget, self).__init__(parent)

handler = MyEventHandler()
mywidget = MyWidget()
mywidget.installEventFilter(handler)

But *most* preferably, would be to not use an event filter, and instead
override the event handler called only on keypress events
<http://doc.qt.io/qt-5/qwidget.html#keyPressEvent>.

  class MyWidget(QtGui.QDialog):
  def __init__(self, parent=None):
    super(MyWidget, self).__init__(parent)

  def keyPressEvent(self, event):
    print("That's a press!")
    return super(MyWidget, self).keyPressEvent(event)

That way, there is zero overhead on any event handling, other than the one
you override.

Hope it helps!
​

On 26 May 2018 at 07:27, Alexey Vasilyev <olex...@gmail.com> wrote:

> Hi.
> I'm new to Maya programming.
> I'm curious if it's possible to track keypress events in Maya main window
> using PySide or any other way?
> For example, when cube primitive is selected, I want to be able to
> increase or decrease its subdivisions using Left and Right arrows. When
> it's not selected, I want arrow keys to function in the default way.
> Thanks.
>
> --
> 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_maya+unsubscr...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/python_inside_maya/c7024d27-4a67-448e-9e57-
> a873ff85dcbc%40googlegroups.com
> <https://groups.google.com/d/msgid/python_inside_maya/c7024d27-4a67-448e-9e57-a873ff85dcbc%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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_maya+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/CAFRtmOAmXDMhNcjQ6H%3DTAK1KYWLRoHE%3DRv3z4CYdt23mNddzMQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to