1

I'm using a script I found here for simulating key presses:

from win32api import keybd_event
import time
import random


Combs = {
'A': [
    'SHIFT',
    'a'],
'B': [
    'SHIFT',
    'b'],
'C': [
    'SHIFT',
    'c'],
'D': [
    'SHIFT',
    'd'],
'E': [
    'SHIFT',
    'e'],
'F': [
    'SHIFT',
    'f'],
'G': [
    'SHIFT',
    'g'],
'H': [
    'SHIFT',
    'h'],
'I': [
    'SHIFT',
    'i'],
'J': [
    'SHIFT',
    'j'],
'K': [
    'SHIFT',
    'k'],
'L': [
    'SHIFT',
    'l'],
'M': [
    'SHIFT',
    'm'],
'N': [
    'SHIFT',
    'n'],
'O': [
    'SHIFT',
    'o'],
'P': [
    'SHIFT',
    'p'],
'R': [
    'SHIFT',
    'r'],
'S': [
    'SHIFT',
    's'],
'T': [
    'SHIFT',
    't'],
'U': [
    'SHIFT',
    'u'],
'W': [
    'SHIFT',
    'w'],
'X': [
    'SHIFT',
    'x'],
'Y': [
    'SHIFT',
    'y'],
'Z': [
    'SHIFT',
    'z'],
'V': [
    'SHIFT',
    'v'],
'Q': [
    'SHIFT',
    'q'],
'?': [
    'SHIFT',
    '/'],
'>': [
    'SHIFT',
    '.'],
'<': [
    'SHIFT',
    ','],
'"': [
    'SHIFT',
    "'"],
':': [
    'SHIFT',
    ';'],
'|': [
    'SHIFT',
    '\\'],
'}': [
    'SHIFT',
    ']'],
'{': [
    'SHIFT',
    '['],
'+': [
    'SHIFT',
    '='],
'_': [
    'SHIFT',
    '-'],
'!': [
    'SHIFT',
    '1'],
'@': [
    'SHIFT',
    '2'],
'#': [
    'SHIFT',
    '3'],
'$': [
    'SHIFT',
    '4'],
'%': [
    'SHIFT',
    '5'],
'^': [
    'SHIFT',
    '6'],
'&': [
    'SHIFT',
    'v'],
'*': [
    'SHIFT',
    '8'],
'(': [
    'SHIFT',
    '9'],
')': [
    'SHIFT',
    '0'] }
Base = {
'0': 48,
'1': 49,
'2': 50,
'3': 51,
'4': 52,
'5': 53,
'6': 54,
'7': 55,
'8': 56,
'9': 57,
'a': 65,
'b': 66,
'c': 67,
'd': 68,
'e': 69,
'f': 70,
'g': 71,
'h': 72,
'i': 73,
'j': 74,
'k': 75,
'l': 76,
'm': 77,
'n': 78,
'o': 79,
'p': 80,
'q': 81,
'r': 82,
's': 83,
't': 84,
'u': 85,
'v': 86,
'w': 87,
'x': 88,
'y': 89,
'z': 90,
'.': 190,
'-': 189,
',': 188,
'=': 187,
'/': 191,
';': 186,
'[': 219,
']': 221,
'\\': 220,
"'": 222,
'ALT': 18,
'TAB': 9,
'CAPSLOCK': 20,
'ENTER': 13,
'BS': 8,
'CTRL': 17,
'ESC': 27,
' ': 32,
'END': 35,
'DOWN': 40,
'LEFT': 37,
'UP': 38,
'RIGHT': 39,
'SELECT': 41,
'PRINTSCR': 44,
'INS': 45,
'DEL': 46,
'LWIN': 91,
'RWIN': 92,
'LSHIFT': 160,
'SHIFT': 161,
'LCTRL': 162,
'RCTRL': 163,
'VOLUP': 175,
'DOLDOWN': 174,
'NUMLOCK': 144,
'SCROLL': 145 }

def KeyUp(Key):
    keybd_event(Key, 0, 2, 0)


def KeyDown(Key):
    keybd_event(Key, 0, 1, 0)


def Press(Key, speed=1):
    rest_time = 0.05/speed
if Key in Base:
    Key = Base[Key]
    KeyDown(Key)
    time.sleep(rest_time)
    KeyUp(Key)
    return True
if Key in Combs:
    KeyDown(Base[Combs[Key][0]])
    time.sleep(rest_time)
    KeyDown(Base[Combs[Key][1]])
    time.sleep(rest_time)
    KeyUp(Base[Combs[Key][1]])
    time.sleep(rest_time)
    KeyUp(Base[Combs[Key][0]])
    return True
return False



def Write(Str, speed = 1):
    for s in Str:
        Press(s, speed)
        time.sleep((0.1 + random.random()/10.0) / float(speed))

I added a to the code a simple script that simulate CTRL+V (paste) method.

def Ctrlv(speed=1):
    rest_time = 0.05/speed
    KeyDown(17) #17 = CTRL
    time.sleep(rest_time)
    KeyDown(86) #86 = V
    KeyUp(17)
    KeyUp(86)

The code runs smoothly and the Ctrlv method paste correctly the clipboard text. The problem is that, after running the script, the CTRL key is still pressed and is not released by the KeyUp event.

Thank you guys.

Andrea
  • 39
  • 6
  • The original code goes in reverse order (releasing the second key followed by the first) so have you tried flipping the order so that you do `KeyUp(86)` first? – SuperBiasedMan Jul 20 '15 at 09:23
  • No, this doesn't work :( – Andrea Jul 20 '15 at 10:52
  • Solved this issue using autoit calling functions (win only): http://stackoverflow.com/questions/3301561/calling-autoit-functions-in-python – Andrea Sep 22 '15 at 09:48

0 Answers0