Not Python itself, but Qt has a way. You probably don't want to run a loop, 
but use Qt's event handler system. To do this, you create an instance of a 
custom Qt class and "install" it as an "event handler" on the main Maya 
window (or on a viewport). Then in the eventFilter() method of your class, 
you look for QKeyEvent, check the key, and if it's the one you want, you 
run your code.

There are a lot of links in that chain, so try something really simple 
first, like printing the event you get. Just be aware that if you print for 
every event (rather than only for QKeyEvent) you will hang Maya because 
there are so many events it will clog up the Script Editor.

This post:
https://groups.google.com/d/msg/python_inside_maya/Auiy06xVqLo/61sqMp_2AQAJ
show an example of putting an event handler on the main Maya window, in 
this case to detect Drop events. You could adjust it to detect keystrokes. 
It was written for Maya 2015 (where the version of Qt was PySide) so you 
may need to adjust it to use PySide2 as well.

On Friday, 9 February 2018 04:12:20 UTC+11, Isai Calderon wrote:
>
> That's exactly what I'd like to do. I'm not too familiar with Qt yet, but 
> I did realize that I could try running a loop through python that could 
> recognize a keypress while the mouse is held down. I could start the python 
> loop while the mouse is pressed down, and end the loop with a 
> mouse-release. The problem then becomes this:
>
> Does Python have a way to detect a keypress?
>
> Thanks!!
>
> On Wednesday, February 7, 2018 at 5:27:04 PM UTC-8, Michael Boon wrote:
>>
>> So your problem is that Maya doesn't notice the hotkey being pressed 
>> while the mouse is down?
>>
>> Maya doesn't appear to have a scriptJob or an MMessage for a key being 
>> pressed.
>>
>> If you're familiar with Qt, you could try using a Qt event handler. I'm 
>> sure you could detect the key being pressed, but there's no guarantee that 
>> you could get Maya to act on it.
>>
>> If you're not familiar with Qt, learning how to set up a handler is a 
>> fair bit of effort for something that may not work at all. Hopefully, 
>> someone else will have a better idea.
>>
>>
>> On Thursday, 8 February 2018 07:09:43 UTC+11, Isai Calderon wrote:
>>>
>>> Hi folks,
>>>
>>> For today's toy, I am attempting to *run a script while the mouse is 
>>> manipulating an object*.
>>>
>>> ```
>>> Use-case:
>>> While moving item, press hotkey to set key and move one frame forward. 
>>> Mouse manipulation is still live on the item.
>>> The hotkey can be repeated indefinitely.
>>> ```
>>>
>>> I am using Pymel, and am looking into a scriptJob to do this, but not 
>>> sure which `Event` or `Condition` to use, or if it should be a new 
>>> `context` tool instead (or combine them)...
>>>
>>> Any thoughts?
>>>
>>> Thank you!
>>>
>>> Isai
>>>
>>

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/75571cf9-a6bc-4eeb-b574-88ec4bdefdcc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to