Menu

Python equivalent of SendKey?

Help
Parker S
2006-03-20
2013-04-23
  • Parker S

    Parker S - 2006-03-20

    Configuring Powermated to perform a "sendKey" event was extremely easy. I'm having trouble finding / scripting an equivalent event within the Gizmod python script config.

    Google hasn't presented much help thus far so I thought I'd give it a shot here. Is it even possible with python?

    Thanks, Parker

     
    • Parker S

      Parker S - 2006-03-20

      After taking a peek at what was providing the sendKey command for Powermated (part of X11.cpp) I noticed that the code was present in GizmoPluginX11, just commented out. Uncommenting only provided me with an error when I tried to make the package, obviously it was commented out for a reason =)

       
    • Parker S

      Parker S - 2006-03-20

      Doubtful that it will be all that useful, but here is the error:

      GizmoPluginX11.cpp:411: error: no 'bool GizmoPluginX11::sendKey(int, bool, bool,                                bool)' member function declared in class 'GizmoPluginX11'
      GizmoPluginX11.cpp:452: error: no 'bool GizmoPluginX11::sendKeyXTest(int, bool,                                bool, bool)' member function declared in class 'GizmoPluginX11'
      GizmoPluginX11.cpp:497: error: no 'bool GizmoPluginX11::sendMouseButton(int, boo                               l)' member function declared in class 'GizmoPluginX11'
      GizmoPluginX11.cpp:514: error: no 'void GizmoPluginX11::scriptFunction_X11GetKey                               Mods(ScriptFunction*)' member function declared in class 'GizmoPluginX11'
      GizmoPluginX11.cpp:544: error: expected unqualified-id before '/' token
      GizmoPluginX11.cpp:544: error: expected constructor, destructor, or type convers                               ion before '/' token

       
    • Parker S

      Parker S - 2006-03-20

      Got everything to compile (I wasn't declaring the method in the hpp file apparently). Now I just need to figure out how to actually call the method properly.

       
    • Tim Burrell

      Tim Burrell - 2006-03-20

      This is no longer supported in gizmod as it is not the best way to perform this function.

      What you do now is send key events to the specific device you want to produce events for.

      For example, if you want to send a key click, you send the key event you want to create to the keyboard plugin.

      It's a good idea to check if that plugin is loaded first, so here's some code that shows this happening:

      DeviceID = GizmoDaemon.isPluginLoadedString("GizmoPluginGeneric", DEVICE_KEYBOARD)
      if DeviceID != -1:
          GizmoDaemon.genericSendKeyEventString(DeviceID, "KEY_N", 1)
          GizmoDaemon.genericSendKeyEventString(DeviceID, "KEY_N", 0)

      The formatting might get messed up but hopefully you get the idea.

      If the plugin is loaded it returns its device id.  Then you use that ID to create the event you want.

      In this case two events are created.  First the letter N is pressed, then it is depressed.

      Make sense?  I know I really need to document the config script... I just haven't had time yet.

       
    • Tim Burrell

      Tim Burrell - 2006-03-20

      I thought I might mention why it's done this way.  This is for maximum flexibility.  In this way you can use the same method for creating mouse events, joystick events, whatever kind of events you can think of.  It also standardizes event generation so that no matter what device you have, if it has an "N" button it'll always generate the code that matches "KEY_N" when that button is pressed.

      This makes it very easy to translate events across devices.

      It also has the added advantage of creating raw device events so it's not dependant upon X.  The events can be sent or caught from the console, or any other place.  AND there are many event types and buttons that X cannot handle.

       
    • Parker S

      Parker S - 2006-03-20

      I was actually just about to post an update. Though it took me a while (that's what I get for not properly scouring the provided config) I did end up with this script (This is just a short snippet)

              if str(Value) == "1.0":
                  if str(DeviceID) == "0.0":
                      DeviceID = GizmoDaemon.isPluginLoadedString("GizmoPluginGeneric", DEVICE_KEYBOARD)
                      GizmoDaemon.genericSendKeyEventString(DeviceID, "KEY_S", 1)
                      GizmoDaemon.genericSendKeyEventString(DeviceID, "KEY_S", 0)
                      debugPrint("Device 0.0 Rotated Up")

      Everything seems to be set up nicely, the only problem being this:

      Loading Generic Plugin [Keyboard]...
              * Failed to Load Generic Plugin for [Keyboard]!

      ..which I don't understand a reason for as I certainly have a keyboard plugged in.

       
      • Tim Burrell

        Tim Burrell - 2006-03-20

        Try running gizmod with the script variable DEBUG set to True.

        When you run gizmod you'll see it print out a list of the devices connected to your computer right after it first starts up.

        Most keyboards have "Keyboard" in their identifier however some do not.  Your keyboard may be one of these devices.

        Note that it is also possible to load the plugin using an absolute path instead of a name match.

        For example instead of:

        GizmoDaemon.loadPlugin("GizmoPluginGeneric", DEVICE_KEYBOARD)

        You can do:

        GizmoDaemon.loadPlugin("GizmoPluginGeneric", "/dev/input/eventX")

        But likely all you'll have to do is change the script variable DEVICE_KEYBOARD to match your system.

        If you still can't get it to work run gizmod in debug mode (-g command line switch) and paste the output.

        Tim.

         
    • Parker S

      Parker S - 2006-03-20

      Worked perfectly, as expected. Thankyou for all your patience and help.

       
    • Fabio Capela

      Fabio Capela - 2006-11-16

      For my application the old X event based SendKey system seem to be the best one.

      I'm planning to use Gizmo Daemon to set a second and a third keyboards so that each is "bound" to one specific window (for use when playing two instances of the same windows game over the network, using Wine). For this purpose I've used the ezmod X input driver to make X respond only to the first keyboard.

      I've had some problems trying to use the approach you've described for some reasons:

      - When I try to capture events from a device configured as an input in X with the evdev driver gizmod don't seem to receive events.
      - I haven't found a way to send events to specific windows with the standard gizmod plugins (although I could somewhat emulate it by changing the focus window, sending the event and restoring the focus window).

      So I've used py-xlib to send key events to specific windows and I've already successfully tested this configuration with one extra keyboard; I'll be extending it to cover the second extra keyboard as soon as I have free time.

      By the way, the extra unconfigured keyboards reset the X server every time "printscreen" or "Control-C" is pressed. This seem to be related to the terminal behind the X server receiving the keypresses. Do you know a way to avoid this problem?

       

Log in to post a comment.