I was wondering if in a UI panel it is possible to capture a keyboard event
I am aware that this can be done in an operator in the invoke function like this:
def invoke(self, context, event):
if event.type == 'A':
print("Example A")
if event.type == 'B':
print("Example B")
But I would like to do it in a panel, and I don't know if it's possible, in case I would like to clear up any doubts about it, I would like to do something like this:
(My example is incorrect, event.type
doesn't exist, but that's for illustration purposes only)
import bpy
class LayoutDemoPanel(bpy.types.Panel):
"""Creates a Panel in the scene context of the properties editor"""
bl_label = "LayoutDemoPanel"
bl_idname = "LayoutDemoPanel"
bl_space_type = "VIEW_3D"
bl_region_type = "UI"
bl_category = "LayoutDemoPanel"
def draw(self, context):
layout = self.layout
# If button 'A' is pressed
if event.type == 'A':
self.layout(text="Example A")
# If button 'B' is pressed
if event.type == 'B':
self.layout(text="Example B")
def register():
bpy.utils.register_class(LayoutDemoPanel)
def unregister():
bpy.utils.unregister_class(LayoutDemoPanel)
if __name__ == "__main__":
register()