Assigning TAB as hotkey

528 views
Skip to first unread message

Marcus Ottosson

unread,
Nov 5, 2012, 10:36:02 AM11/5/12
to python_in...@googlegroups.com
Maya doesn't seem to support it via it's hotkey editor. I'm a Qt user and have tried taking ownership of the tab key via overriding maya's own keyPressEvent() without luck, I suspect it might work in versions prior to 2013 though due to their own usage of the tab-key in the node editor, although I have yet to confirm this.

Anyone have any ideas? Preferably I'd like to keep the native tab functionality of switching between text- and num-fields and only have it active when regular hotkeys are active.

Best,
Marcus

Judah Baron

unread,
Nov 5, 2012, 1:22:22 PM11/5/12
to python_inside_maya
You can install an event filter on any QObject. When you want to capture the tab key, say while a particular window is open, you could install an event filter on that window. This window will now receive events that you will handle in a custom fashion with your code.


Make sure to either accept() or ignore() your event once it gets to your handler. This will ensure correct propagation, so other subscribers, such as you point out, will receive the event in the case you are not interested in it.

-Judah




Marcus Ottosson

unread,
Nov 7, 2012, 9:28:54 AM11/7/12
to python_in...@googlegroups.com
Thanks for your response. I should've mentioned, I did try installing an event filter, but it reported events from essentially ALL keys, EXCEPT tab. :)

I'll give it another go tonight. Can you think of any other way to hook into the tab key?

Judah Baron

unread,
Nov 7, 2012, 12:43:51 PM11/7/12
to python_inside_maya
I'm guessing you need to go higher up in the object hierarchy with the event filter. There is probably something else swallowing that event so you never receive it.


On Wed, Nov 7, 2012 at 6:28 AM, Marcus Ottosson <konstr...@gmail.com> wrote:
Thanks for your response. I should've mentioned, I did try installing an event filter, but it reported events from essentially ALL keys, EXCEPT tab. :)

I'll give it another go tonight. Can you think of any other way to hook into the tab key?

Justin Israel

unread,
Nov 7, 2012, 1:33:07 PM11/7/12
to python_in...@googlegroups.com
Check this out. You can create a QAction that is set to the Application context, and then attach it to the MainWindow:

####
from PyQt4 import QtCore, QtGui
import sip

import maya.OpenMayaUI as mui

mainWin = sip.wrapinstance(long(mui.MQtUtil.mainWindow()), QtGui.QWidget)

action = QtGui.QAction(mainWin)
action.setShortcut(QtGui.QKeySequence(QtCore.Qt.Key_Tab))
action.setShortcutContext(QtCore.Qt.ApplicationShortcut)

def foo():
    print "TAB!"
    
action.triggered.connect(foo)
mainWin.addAction(action)
####

Whenever I press TAB with the MainWindow in focus, I get the action triggered. 
Now you could either save the reference to the action and slot, and disconnect/reconnect to new ones for hot key references...or..you could just set this all up once and have the slot call a function on your class that you can simply replace at any time. That way you never have to mess with the action or original slot. You just update whatever function the slot calls.

-- justin

Marcus Ottosson

unread,
Nov 11, 2012, 10:38:42 AM11/11/12
to python_in...@googlegroups.com
Works perfectly, thanks Justin!

Marcus

Mike Bourbeau

unread,
Jun 12, 2015, 3:58:44 PM6/12/15
to python_in...@googlegroups.com
What should I do if I want to open a tool with TAB, but then have TAB be a hotkey that is used within that tool's UI? 

EX: Maya starts>>> you open the tool with TAB>>> type in a word into a QLineEdit box>>> highlight a word in a QCompleter by using the arrow keys >>> then use TAB to select that word. I know how to do everything else, I just don't have a clue about actions and how they work. 

 Also, how would I make it so TAB is taken over every time Maya is opened?  I know this might require a script job, but last time I tried to create a script job the commands were executed before maya was even fully opened, so they did nothing. 
Message has been deleted

Mike Bourbeau

unread,
Jun 12, 2015, 4:00:44 PM6/12/15
to python_in...@googlegroups.com
from PySide import QtCore, QtGui
from shiboken import wrapInstance 
import maya.OpenMayaUI as mui

mainWin = wrapInstance(long(mui.MQtUtil.mainWindow()), QtGui.QWidget)

action = QtGui.QAction(mainWin)
action.setShortcut(QtGui.QKeySequence(QtCore.Qt.Key_Tab))
action.setShortcutContext(QtCore.Qt.ApplicationShortcut)

def foo():
    print "TAB!"
    
action.triggered.connect(foo)
mainWin.addAction(action)

borb...@gmail.com

unread,
Jun 12, 2015, 4:38:41 PM6/12/15
to python_in...@googlegroups.com
PySide version:

#####################################################
from PySide import QtCore, QtGui
from shiboken import wrapInstance
import maya.OpenMayaUI as mui

mainWin = wrapInstance(long(mui.MQtUtil.mainWindow()), QtGui.QWidget)

action = QtGui.QAction(mainWin)
action.setShortcut(QtGui.QKeySequence(QtCore.Qt.Key_Tab))
action.setShortcutContext(QtCore.Qt.ApplicationShortcut)

def foo():
print "TAB!"

action.triggered.connect(foo)
mainWin.addAction(action)
#####################################################

On Sunday, November 11, 2012 at 10:38:42 AM UTC-5, Marcus Ottosson wrote:

Martin Gunnarsson

unread,
Feb 22, 2016, 5:55:42 PM2/22/16
to Python Programming for Autodesk Maya
Nice solution!

Unfortunately it overrides the default behaviour of tab in text-fields and the Node-Editor.
Wonder if there is a way around that....

Geordie Martinez

unread,
Feb 23, 2016, 12:45:16 AM2/23/16
to python_inside_maya

in the pyside docs here is what is said about using TAB for both the default behavior (focusing on buttons) and capturing the keypress;
https://deptinfo-ensip.univ-poitiers.fr/ENS/pyside-docs/PySide/QtGui/QWidget.html

I’m betting someone out there has reimplemented the QWidget.event method

DOCS:

Widgets that accept keyboard input need to reimplement a few more event handlers:

PySide.QtGui.QWidget.keyPressEvent() is called whenever a key is pressed, and again when a key has been held down long enough for it to auto-repeat. The Tab and Shift+Tab keys are only passed to the widget if they are not used by the focus-change mechanisms. To force those keys to be processed by your widget, you must reimplement QWidget.event() .


--
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 view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/04dd609c-91e4-45c7-bbaf-73713e14e9e3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Zhen Huang

unread,
Dec 14, 2018, 12:05:55 AM12/14/18
to Python Programming for Autodesk Maya
Hey ,

I read this today , so great have your idea about this topic. just update to you guys what I found , I think in most of case we just want to make the hotkey available in view port , so I did a modification which only assign the tab hotkey in view port instead of replace everything in whole maya application .

It's working good without touch node editor or textField , have fun:


import maya.cmds as cmd

import maya.OpenMayaUI as mui
from shiboken2 import wrapInstance

from PySide2.QtGui import *
from PySide2.QtCore import *
from PySide2.QtWidgets import *

def tab_key_test():
  cmd.polyCube()

### get main panel from name 
MainPane = wrapInstance(long(mui.MQtUtil.findLayout('MainPane')) , QWidget )
### create q action
action = QAction(MainPane)
### set short cut key to tab
action.setShortcut(QKeySequence(Qt.Key_Tab))
### set context to whildren of MainPane
action.setShortcutContext(Qt.WidgetWithChildrenShortcut)
### connect to custom function
action.triggered.connect(tab_key_test)
### add action to widget
MainPane.addAction(action)


Best , 

在 2012年11月5日星期一 UTC+8下午11:36:02,Marcus Ottosson写道:
Reply all
Reply to author
Forward
0 new messages