+ Reply to Thread
Results 1 to 7 of 7

Thread: Keypress event in python,Plz?

  1. #1

    Keypress event in python,Plz?

    hey guys, i just wonder whether there is a way to do keypress event when the python script is runing? for instance, while the script is runing, i am able to get a single frame output by pressing 'R' on the keyboard, rather than looping through every frame as output , any thought or ideas? thanks in advance

    ting

  2. #2
    Senior Member Steve Baer's Avatar
    Join Date
    Apr 2010
    Location
    Seattle
    Posts
    1,472
    Blog Entries
    19
    Hi Ting,
    I don't quite understand what you are trying to do. Maybe you want to try the GetString function to pause Rhino for user input?
    Thanks,
    -Steve

  3. #3
    Quote Originally Posted by Steve Baer View Post
    Hi Ting,
    I don't quite understand what you are trying to do. Maybe you want to try the GetString function to pause Rhino for user input?
    Thanks,
    -Steve
    Hi Steve,
    thank you so much for the quick reply, am afraid GetString() is not exactly what am looking for, the function am looking for is that when the script is running, by pressing any key on the keyboard, am able to call a function( functions like rs.CreatePreviewImage(),so i can output a single image), once the function is done, the script will resume runing where it leftover until the end, for instance, i have a animation script in python which is a ball bouncing around, rather than setting up where to record in advance, i prefer to record any moment i feel interested interactivly while the script is running, i think it is very similar to the function scriptcontext.escape_test(), the difference is i would like to trigger my customized function rather than breaking the script. thank you so much again
    cheers
    ting
    Last edited by tingting; 08-27-2012 at 07:35 PM.

  4. #4

    A Windows only solution through ctypes

    Remark i am pretty sure there is a possibility for doing this in a pure .NET way but anyhow on windows this will work:
    Code:
    import ctypes
    from ctypes.wintypes import WORD
    user32 = ctypes.windll.user32
    user32.GetAsyncKeyState.restype = WORD
        
    while True:
        user32.GetAsyncKeyState.argtypes = [ ctypes.c_char ]
        if user32.GetAsyncKeyState('Q'): break
        if user32.GetAsyncKeyState('A'): print "you"*100

  5. #5
    Thank you so much for your help ,i got it.

  6. #6
    HI All .Thanks for this code.I tried it but it happened to me something strange:when i write "A" on the keyboard the script breaks ,but in the editor i find "A" written in the position of the curson where it was when i ran the script
    Attached Files
    Last edited by jonny; 11-02-2012 at 09:07 AM.

  7. #7
    yep I can reproduce this nasty behaviour. The RhinoPython Editor still handles all Keyboard events when the script is started from within.
    You could create a toolbar button starting the Command -RunPythonScript "yourkeyboradscript.py" and than start your script execution from that button.
    More proper but more coding: You have to create a dialog eating up the keyboard events in a more conventional way.

+ Reply to Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts