Re: SendKeys and Python 2.7

Discussion in 'Python' started by Michel Claveau - MVP, Sep 9, 2010.

  1. Hi!

    Example for send ^V (with PyWin32):

    import time,win32api,win32con
    win32api.keybd_event(win32con.VK_CONTROL, 0, 0, 0)
    win32api.keybd_event(ord('V'), 0, win32con.KEYEVENTF_EXTENDEDKEY | 0, 0)
    time.sleep(0.05)
    win32api.keybd_event(ord('V'), 0, win32con.KEYEVENTF_EXTENDEDKEY | win32con.KEYEVENTF_KEYUP, 0)
    win32api.keybd_event(win32con.VK_CONTROL, 0, win32con.KEYEVENTF_KEYUP, 0)



    @-salutations
    --
    Michel Claveau
     
    Michel Claveau - MVP, Sep 9, 2010
    #1
    1. Advertising

  2. On Thu, Sep 9, 2010 at 5:40 PM, Michel Claveau - MVP
    <> wrote:
    > Hi!
    >
    > Example for send ^V  (with PyWin32):
    >
    >  import time,win32api,win32con
    >  win32api.keybd_event(win32con.VK_CONTROL, 0, 0, 0)
    >  win32api.keybd_event(ord('V'), 0, win32con.KEYEVENTF_EXTENDEDKEY | 0, 0)
    >  time.sleep(0.05)
    >  win32api.keybd_event(ord('V'), 0, win32con.KEYEVENTF_EXTENDEDKEY | win32con.KEYEVENTF_KEYUP, 0)
    >  win32api.keybd_event(win32con.VK_CONTROL, 0, win32con.KEYEVENTF_KEYUP, 0)


    Thank you very much! Your code solved my problem. I added some lines
    to set the focus into R before the ^V and then back to Vim:

    import time,win32api,win32con,win32com.client

    shell = win32com.client.Dispatch("WScript.Shell")
    shell.AppActivate("R Console")
    win32api.keybd_event(win32con.VK_CONTROL, 0, 0, 0)
    win32api.keybd_event(ord('V'), 0, win32con.KEYEVENTF_EXTENDEDKEY | 0, 0)
    time.sleep(0.05)
    win32api.keybd_event(ord('V'), 0, win32con.KEYEVENTF_EXTENDEDKEY |
    win32con.KEYEVENTF_KEYUP, 0)
    win32api.keybd_event(win32con.VK_CONTROL, 0, win32con.KEYEVENTF_KEYUP, 0)
    shell.AppActivate("Vim")

    Best regards,

    Jakson Aquino
     
    Jakson A. Aquino, Sep 10, 2010
    #2
    1. Advertising

  3. On Thu, Sep 9, 2010 at 8:24 PM, Jakson A. Aquino <> wrote:
    > On Thu, Sep 9, 2010 at 5:40 PM, Michel Claveau - MVP
    > <> wrote:
    >> Hi!
    >>
    >> Example for send ^V  (with PyWin32):
    >>
    >>  import time,win32api,win32con
    >>  win32api.keybd_event(win32con.VK_CONTROL, 0, 0, 0)
    >>  win32api.keybd_event(ord('V'), 0, win32con.KEYEVENTF_EXTENDEDKEY | 0, 0)
    >>  time.sleep(0.05)
    >>  win32api.keybd_event(ord('V'), 0, win32con.KEYEVENTF_EXTENDEDKEY | win32con.KEYEVENTF_KEYUP, 0)
    >>  win32api.keybd_event(win32con.VK_CONTROL, 0, win32con.KEYEVENTF_KEYUP, 0)

    >
    > Thank you very much! Your code solved my problem. I added some lines
    > to set the focus into R before the ^V and then back to Vim:


    Unfortunately, I was wrong. Your code do send the ^v as expected, but
    I have problem with the selection of the Windows which will receive
    the ^v. The code above was OK in a Windows XP running inside
    VirtualBox, but when tested in a real machine, it proved to be highly
    inconsistent. Sometimes the ^v gets pasted into R, but more frequently
    it is pasted into Vim itself or nowhere. Below is the complete code
    that I'm using. It's a vim script. The python code is delimited by
    "python << EOL" and "EOL":

    function! SendToRPy(aString)
    python << EOL
    import time
    import win32api
    import win32con
    import win32com.client
    import win32clipboard
    import vim

    aString = vim.eval("a:aString")
    win32clipboard.OpenClipboard()
    win32clipboard.EmptyClipboard()
    win32clipboard.SetClipboardText(aString)
    win32clipboard.CloseClipboard()
    shell = win32com.client.Dispatch("WScript.Shell")
    ok = shell.AppActivate("R Console")
    if ok:
    win32api.keybd_event(win32con.VK_CONTROL, 0, 0, 0)
    win32api.keybd_event(ord('V'), 0, win32con.KEYEVENTF_EXTENDEDKEY | 0, 0)
    time.sleep(0.05)
    win32api.keybd_event(ord('V'), 0, win32con.KEYEVENTF_EXTENDEDKEY |
    win32con.KEYEVENTF_KEYUP, 0)
    win32api.keybd_event(win32con.VK_CONTROL, 0, win32con.KEYEVENTF_KEYUP, 0)
    shell.AppActivate("Vim")
    else:
    vim.command("call RWarningMsg('Is R running?')")
    time.sleep(1)
    EOL
    endfunction

    When R isn't running, the script correctly shows the warning message
    "Is R running?". Does anyone know what should I do to correctly use
    the AppActivate function or is there a better approach to this
    problem?

    Thanks!

    Jakson Aquino
     
    Jakson A. Aquino, Sep 11, 2010
    #3
  4. Michel Claveau - MVP

    MRAB Guest

    On 11/09/2010 01:45, Jakson A. Aquino wrote:
    > On Thu, Sep 9, 2010 at 8:24 PM, Jakson A. Aquino<> wrote:
    >> On Thu, Sep 9, 2010 at 5:40 PM, Michel Claveau - MVP
    >> <> wrote:
    >>> Hi!
    >>>
    >>> Example for send ^V (with PyWin32):
    >>>
    >>> import time,win32api,win32con
    >>> win32api.keybd_event(win32con.VK_CONTROL, 0, 0, 0)
    >>> win32api.keybd_event(ord('V'), 0, win32con.KEYEVENTF_EXTENDEDKEY | 0, 0)
    >>> time.sleep(0.05)
    >>> win32api.keybd_event(ord('V'), 0, win32con.KEYEVENTF_EXTENDEDKEY | win32con.KEYEVENTF_KEYUP, 0)
    >>> win32api.keybd_event(win32con.VK_CONTROL, 0, win32con.KEYEVENTF_KEYUP, 0)

    >>
    >> Thank you very much! Your code solved my problem. I added some lines
    >> to set the focus into R before the ^V and then back to Vim:

    >
    > Unfortunately, I was wrong. Your code do send the ^v as expected, but
    > I have problem with the selection of the Windows which will receive
    > the ^v. The code above was OK in a Windows XP running inside
    > VirtualBox, but when tested in a real machine, it proved to be highly
    > inconsistent. Sometimes the ^v gets pasted into R, but more frequently
    > it is pasted into Vim itself or nowhere. Below is the complete code
    > that I'm using. It's a vim script. The python code is delimited by
    > "python<< EOL" and "EOL":
    >
    > function! SendToRPy(aString)
    > python<< EOL
    > import time
    > import win32api
    > import win32con
    > import win32com.client
    > import win32clipboard
    > import vim
    >
    > aString = vim.eval("a:aString")
    > win32clipboard.OpenClipboard()
    > win32clipboard.EmptyClipboard()
    > win32clipboard.SetClipboardText(aString)
    > win32clipboard.CloseClipboard()
    > shell = win32com.client.Dispatch("WScript.Shell")
    > ok = shell.AppActivate("R Console")
    > if ok:
    > win32api.keybd_event(win32con.VK_CONTROL, 0, 0, 0)
    > win32api.keybd_event(ord('V'), 0, win32con.KEYEVENTF_EXTENDEDKEY | 0, 0)
    > time.sleep(0.05)
    > win32api.keybd_event(ord('V'), 0, win32con.KEYEVENTF_EXTENDEDKEY |
    > win32con.KEYEVENTF_KEYUP, 0)
    > win32api.keybd_event(win32con.VK_CONTROL, 0, win32con.KEYEVENTF_KEYUP, 0)
    > shell.AppActivate("Vim")
    > else:
    > vim.command("call RWarningMsg('Is R running?')")
    > time.sleep(1)
    > EOL
    > endfunction
    >
    > When R isn't running, the script correctly shows the warning message
    > "Is R running?". Does anyone know what should I do to correctly use
    > the AppActivate function or is there a better approach to this
    > problem?
    >

    I'd add some more small sleeps to give Windows/R time to act, IYSWIM. I
    learned that from experience. :)
     
    MRAB, Sep 11, 2010
    #4
  5. In message <>, Jakson A.
    Aquino wrote:

    > Your code do send the ^v as expected, but I have problem with the
    > selection of the Windows which will receive the ^v. The code above was OK
    > in a Windows XP running inside VirtualBox, but when tested in a real
    > machine, it proved to be highly inconsistent. Sometimes the ^v gets pasted
    > into R, but more frequently it is pasted into Vim itself or nowhere.


    No big surprise your roundabout GUI-fiddling approach is flaky.
     
    Lawrence D'Oliveiro, Sep 11, 2010
    #5
  6. On Sat, Sep 11, 2010 at 4:25 AM, Lawrence D'Oliveiro
    <_zealand> wrote:
    > In message <>, Jakson A.
    > Aquino wrote:
    >
    >> Your code do send the ^v as expected, but I have problem with the
    >> selection of the Windows which will receive the ^v. The code above was OK
    >> in a Windows XP running inside VirtualBox, but when tested in a real
    >> machine, it proved to be highly inconsistent. Sometimes the ^v gets pasted
    >> into R, but more frequently it is pasted into Vim itself or nowhere.

    >
    > No big surprise your roundabout GUI-fiddling approach is flaky.


    Thanks for your comment, but could you please suggest a feasible and
    non flaky approach?
     
    Jakson A. Aquino, Sep 11, 2010
    #6
  7. On Fri, Sep 10, 2010 at 10:38 PM, MRAB <> wrote:
    > I'd add some more small sleeps to give Windows/R time to act, IYSWIM. I
    > learned that from experience. :)


    I've tried adding sleeps, small and large, but they didn't make much
    of a difference. Anyway, different machines could require different
    amounts of sleep and, thus, this workaround may not be generalizable
    enough since the plugin would be used by many people on different
    systems.

    Thanks,

    Jakson
     
    Jakson A. Aquino, Sep 11, 2010
    #7
  8. In message <>, Jakson A.
    Aquino wrote:

    > On Sat, Sep 11, 2010 at 4:25 AM, Lawrence D'Oliveiro
    > <_zealand> wrote:
    >
    >> In message <>, Jakson
    >> A. Aquino wrote:
    >>
    >>> Your code do send the ^v as expected, but I have problem with the
    >>> selection of the Windows which will receive the ^v. The code above was
    >>> OK in a Windows XP running inside VirtualBox, but when tested in a real
    >>> machine, it proved to be highly inconsistent. Sometimes the ^v gets
    >>> pasted into R, but more frequently it is pasted into Vim itself or
    >>> nowhere.

    >>
    >> No big surprise your roundabout GUI-fiddling approach is flaky.

    >
    > Thanks for your comment, but could you please suggest a feasible and
    > non flaky approach?


    I already did.
     
    Lawrence D'Oliveiro, Sep 11, 2010
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mike John

    How do I sendkeys

    Mike John, Jul 7, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    520
    Mike John
    Jul 7, 2003
  2. oyster
    Replies:
    0
    Views:
    559
    oyster
    Aug 10, 2010
  3. Albert Hopkins

    Re: SendKeys and Python 2.7

    Albert Hopkins, Sep 9, 2010, in forum: Python
    Replies:
    0
    Views:
    447
    Albert Hopkins
    Sep 9, 2010
  4. Jakson A. Aquino

    Re: SendKeys and Python 2.7

    Jakson A. Aquino, Sep 10, 2010, in forum: Python
    Replies:
    3
    Views:
    673
    Lawrence D'Oliveiro
    Sep 11, 2010
  5. Michel Claveau - MVP

    Re: SendKeys and Python 2.7

    Michel Claveau - MVP, Sep 13, 2010, in forum: Python
    Replies:
    1
    Views:
    1,370
    Jakson A. Aquino
    Sep 18, 2010
Loading...

Share This Page