onKeyEvent issue (python)

   524   2   4
User Avatar
Member
55 posts
Joined: March 2016
Offline
hi

I am writing a HDA python viewer state.
I would like to do something when the "r" key is pressed.
I wrote this python line to do that:

self.key_pressed = ui_event.device().keyString()
if self.key_pressed == "r":
    self.node.parm("color").set(0)

but when I test the HDA and press "r", it active the "rotate" tool instead of staying in my view state.

how can I avoid it?

Thanks
Edited by Gerardo Castellanos - Sept. 16, 2024 16:52:49
Gerardo Castellanos
www.gerardocastellanos.com
User Avatar
Member
135 posts
Joined: Aug. 2010
Offline
Houdini has a bunch of magical keys(ERTYS) locked into their viewport/modeling/edit workflow that for some reason has priority over everything else in the software and can’t be overridden.

I hoped maybe the new hotkey system with 20.5 would free these keys but I haven’t looked into this yet. Otherwise tough luck, send another RFE, SideFX knows but it might take a few years for things to change.
Edited by aeaeaeae - Sept. 17, 2024 03:10:36
B-System for Houdini [ae43ae43.gumroad.com]: instance editor, blender like interface.
User Avatar
Staff
1081 posts
Joined: July 2005
Offline
The new hotkey system in 20.5 does indeed give state hotkey contexts priority during hotkey resolution. However, to take advantage of that, you need to use the hotkey system, i.e., register a hotkey context symbol, hotkey action symbols, and default key bindings for that hotkey context. Without that you're at the mercy of the hard-coded order in which various key event handlers are triggered by Houdini. It also good practice to do in general so that key bindings can be customized through the hotkey manager.

I think the examples/documentation are still a little behind in this respect, so the easiest way to see an example is to look at one of the python states we're shipping. For example, $HFS/houdini/viewer_states/sidefx_clip.py. The way it's done there is designed to work under both the old and new hotkey systems in 20.0 and up, though of course the new system is only used by 20.5 and up.

What you're looking for is the use of the `hou.PluginHotkeyDefinitions` object to register the various hotkey symbols and default bindings with the viewer state template in `createViewerStateTemplate()`.

In the `onKeyEvent()` function, you'll want to note how `viewerstate.utils.hotkeySymbolOrKeyString()` is used instead of the raw key string from the `ui_event`, and compared against a hotkey symbol using `hou.hotkeys.isKeyMatch()`.
Edited by Ondrej - Sept. 19, 2024 10:24:33
  • Quick Links