# How do I catch keyboard input for a blender plugin?

I'm writing a leap motion blender input plugin, largely because the existing efforts seem to have stalled.

One of the problematic issues with the leap is that after moving / scaling / rotating an object, there's no good way to 'stop' the leap input. Even using both hand, the leap often awkwardly tracks tiny jitters as you change gesture or add/remove hands.

The best solution seems to be to control the input of the leap by keyboard input; eg. hold down control to designate input mode, and release it to 'drop' the object or stop rotating the camera.

However, I'm having trouble trying to catch this sort of input from the plugin api.

I've looked at bpy.context.window_manager.keyconfigs, but it doesn't seem helpful.

How can I catch the input of keydown / keyup on control keys?

It's worth noting that I have explored the idea of adding new keyconfig's to 'start leap mode' and 'stop leap mode' using operators (eg. cntl -Z start, cntl-shift-Z stop, or whatever) but finding free easy-to-press key bindings is really tricky and it is, practically speaking, really awkward. So uh... if you were going to suggest that, dont? It's not really a good solution.

(Incidentally, the existing efforts at leap bindings were all tease and no action, frustratingly; that sucks. If you want to see what I've done, such as it is, the source is located here: https://github.com/shadowmint/leap-blender)

• Turns out your code already use modal operator, but I don't understand why you use timer instead of directly checking keyboard event. – Adhi Oct 22 '13 at 15:50
• The reason is that the leap input is a long polling operation; if you just poll constantly for input you effectively block the UI forever waiting for input, which makes it rather useless as an input method. However, your approach may be possible to use, I'll have to experiment. – Doug Oct 23 '13 at 1:10

I have no means to test for your intended usage, but maybe a modal operator is viable. Based on your use case, here's an operator template you could modify:

class VIEW3D_OT_process_input(bpy.types.Operator):
"""Process input while Control key is pressed."""
bl_idname = 'view3d.process_input'
bl_label = 'Process Input'
bl_options = {'REGISTER'}

def modal(self, context, event):
if event.type == 'ESC':
return {'FINISHED'}
elif event.ctrl:
pass # Input processing code.

return {'PASS_THROUGH'}

def invoke(self, context, event):

Watching input event being passed as argument, the operator will keep running (by returning PASS_THROUGH) until a condition, in this case pressing Esc button, is met. The input processing code itself is only executed if Ctrl key is pressed.