Keyboard interruption in Python

Hello everyone. I’m starting to learn python so please excuse my simply question. I’m using python 2.4, Blender 2.41 on WinXp platform. The question is how to stop a simple loop with a key event? This is the code example:

import Blender
from Blender import Object
from Blender.BGL import *
from Blender.Draw import *
import msvcrt

EXIT = 1

object=Blender.Object.Get(“Cube”)

def main():
Button(“Exit”, EXIT, 100,100,200,18)

def event(evt, val):

if (evt == QKEY):
    Exit()
    
elif (evt == DKEY):
    move()

Register(main, event)

def move():

for i in range (1, 100)
    i+=1
    object.LocX+=0.1
    Blender.Redraw()

I need to stop a move function with a specific key interruption. I already tried KeyInterruption exception and msvcrt library but nothing works. It seems not to read any hardware input while loop executing. Maybe I was doing something wrong. If anyone have any idea please give me an example.
Also If somebody can tell me how to start an event only on pressing a key down without using a pygame module would be great.
(explanation:
if evt==WKEY:
object.LocX+=0.1
In fact object moves for 0.2 )

That’s the exact same problem as I’m having:

http://blenderartists.org/forum/showthread.php?t=66901

stiv mentioned spacehandlers but as usual the docs are kinda vague as to how to use them. Overall, I get the feeling the Python API is a bit experimental. Maybe after the recode, things will be a bit more solid to work from.

I had some success with Blender.event but it worked properly and then didn’t for some reason. I’ll do some more experimenting. Threading seems to not work either, which is a shame because I was hoping to use threads quite a lot.

I found some links online about it (not Blender related) that suggested using msvcrt. One thing to bear in mind if you are making cross-platform code is that msvcrt is Windows-only. Did you try a msvcrt.getch() call in your loop? If you put that after each iteration, it should check the event queue to see what key has been pressed and you handle it inside the loop.

Snowden: isn’t it possible to include a condition inside the loop checking for user input?

osxrules: I had a first look at spacehandlers myself a few days ago. This page really helped me out. (at about 1/3 of the page, with the heading “Space Handler script links:”)

Thanks, that helps. I don’t think space handlers are what I want now. OK, maybe they are supposed to be cleaner but I don’t like the idea of having to manually enable event listening in the 3D window and having to explain that to users so the script works properly nor have it limited to certain windows. I think I’ll keep looking down the route of using either QRead or some way to do it purely with the Python API.

You could do something like:


if Window.QTest():
     evt, val = Window.QRead()

and then use evt, val as in your event function (even call it with these arguments).

mscvrt will probably not work in Blender BTW.

yes!! Window.QTest is working. not exactly as I was expecting but it’s good enough And I figured out how to make python react only on pressing key down:
if (evt==DKEY) and (val==1):

Thanks nikosg