'event' and 'full_event' parameters allow to get a keystroke value.
- 'event': a key with no modifier
- 'full_event': a key with modifiers

Here is a sample code:
import bpy
class HelloWorldPanel(bpy.types.Panel):
bl_label = "Hello World Panel"
bl_idname = "OBJECT_PT_hello"
bl_space_type = 'PROPERTIES'
bl_region_type = 'WINDOW'
bl_context = "object"
keymaps = []
keymapitem = None
def draw(self, context):
layout = self.layout
obj = context.object
#Allow to get a key on a locally defined property
layout.row().prop( obj, "myProperty", event = True )
#Allow to get a key: no, does not work
layout.row().prop( obj, "myProperty", full_event=True )
#Allow to get a key on an existing property
layout.row().prop( context.scene.game_settings, "exit_key", event=True )
#Allow to get a full event key on a keymap item
layout.row().prop( HelloWorldPanel.keymapitem, "type", full_event=True )
def register():
#Enum property to define the allowed keys for this property
bpy.types.Object.myProperty = bpy.props.EnumProperty( items=[('A','A',''),('LEFT_CTRL','LEFT_CTRL',''),('LEFT_ALT','LEFT_ALT','')] )
bpy.utils.register_class(HelloWorldPanel)
#Create a keymap and a key
wm = bpy.context.window_manager
km = wm.keyconfigs.addon.keymaps.new(name='Test', space_type='EMPTY')
kmi = km.keymap_items.new(HelloWorldPanel.bl_idname, 'LEFT_SHIFT', 'PRESS')
#Store them here
HelloWorldPanel.keymaps.append(km)
HelloWorldPanel.keymapitem = kmi
def unregister():
bpy.utils.unregister_class(HelloWorldPanel)
del bpy.types.Object.myProperty
wm = bpy.context.window_manager
for km in HelloWorldPanel.keymaps:
wm.keyconfigs.addon.keymaps.remove(km)
del HelloWorldPanel.keymaps[:]
if __name__ == "__main__":
register()
Some explanations and remarks:
For a locally defined property (myProperty in the code above): the property is an enum which defines the scope of the possible values. But it seems that the user will not be warned if she/he choose a not wanted value.
You can use it for built in properties, like 'context.scene.game_settings' above.
But in both previous cases, I've found no solution to use 'full_event'.
- Last example using a keymap item. Here choosing its type as attribute allows the usage of 'full_event'.
I don't know why some are working with 'full_event' and not others, as the enum described in the documentation seems to be the same. See the documentation here.
Ctrl
,Shift
,Space
$\endgroup$