Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: SendKeys and Python 2.7

Reply
Thread Tools

Re: SendKeys and Python 2.7

 
 
Michel Claveau - MVP
Guest
Posts: n/a
 
      09-09-2010
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
 
Reply With Quote
 
 
 
 
Jakson A. Aquino
Guest
Posts: n/a
 
      09-09-2010
On Thu, Sep 9, 2010 at 5:40 PM, Michel Claveau - MVP
<(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Jakson A. Aquino
Guest
Posts: n/a
 
      09-11-2010
On Thu, Sep 9, 2010 at 8:24 PM, Jakson A. Aquino <(E-Mail Removed)> wrote:
> On Thu, Sep 9, 2010 at 5:40 PM, Michel Claveau - MVP
> <(E-Mail Removed)> 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
 
Reply With Quote
 
MRAB
Guest
Posts: n/a
 
      09-11-2010
On 11/09/2010 01:45, Jakson A. Aquino wrote:
> On Thu, Sep 9, 2010 at 8:24 PM, Jakson A. Aquino<(E-Mail Removed)> wrote:
>> On Thu, Sep 9, 2010 at 5:40 PM, Michel Claveau - MVP
>> <(E-Mail Removed)> 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.
 
Reply With Quote
 
Lawrence D'Oliveiro
Guest
Posts: n/a
 
      09-11-2010
In message <(E-Mail Removed)>, 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.
 
Reply With Quote
 
Jakson A. Aquino
Guest
Posts: n/a
 
      09-11-2010
On Sat, Sep 11, 2010 at 4:25 AM, Lawrence D'Oliveiro
<(E-Mail Removed)_zealand> wrote:
> In message <(E-Mail Removed)>, 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?
 
Reply With Quote
 
Jakson A. Aquino
Guest
Posts: n/a
 
      09-11-2010
On Fri, Sep 10, 2010 at 10:38 PM, MRAB <(E-Mail Removed)> 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
 
Reply With Quote
 
Lawrence D'Oliveiro
Guest
Posts: n/a
 
      09-11-2010
In message <(E-Mail Removed)>, Jakson A.
Aquino wrote:

> On Sat, Sep 11, 2010 at 4:25 AM, Lawrence D'Oliveiro
> <(E-Mail Removed)_zealand> wrote:
>
>> In message <(E-Mail Removed)>, 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: SendKeys and Python 2.7 Michel Claveau - MVP Python 1 09-17-2010 11:04 PM
Re: SendKeys and Python 2.7 Jakson A. Aquino Python 3 09-11-2010 07:24 AM
Re: SendKeys and Python 2.7 Albert Hopkins Python 0 09-09-2010 11:51 AM
how to let shell.SendKeys use unicode on windows in python oyster Python 0 08-10-2010 03:04 AM
How do I sendkeys Mike John ASP .Net 0 07-07-2003 01:14 PM



Advertisments