Verbessertes SendKeys mit Tasten gedrückhalten

Gute Links und Tutorials könnt ihr hier posten.
Antworten
INFACT
User
Beiträge: 385
Registriert: Freitag 5. Dezember 2008, 16:08

Ich habe in letzter Zeit mal mit SendKeys herumgespielt und und habe mich ein bisschen darüber geärgert, dass man die tasten nicht gedrückt halten kann.
Dann habe ich mir das Prog anggeguckt und herumgespielt.
wenn man :

Code: Alles auswählen

def playkeys(keys, pause=.05):
    """
    Simulates pressing and releasing one or more keys.

    `keys` : str
        A list of 2-tuples consisting of ``(keycode,down)``
        where `down` is `True` when the key is being pressed
        and `False` when it's being released.

        `keys` is returned from `str2keys`.
    `pause` : float
        Number of seconds between releasing a key and pressing the 
        next one.
    """
    for (vk, arg) in keys:
        if vk:
            if arg:
                key_down(vk)
            else:
                key_up(vk)
                if pause:   # pause after key up
                    time.sleep(pause)
        else:
            time.sleep(arg)


def SendKeys(keys, 
             pause=0.05, 
             with_spaces=False, 
             with_tabs=False, 
             with_newlines=False,
             turn_off_numlock=True):
    """
    Sends keys to the current window.

    `keys` : str
        A string of keys.
    `pause` : float
        The number of seconds to wait between sending each key
        or key combination.
    `with_spaces` : bool
        Whether to treat spaces as ``{SPACE}``. If `False`, spaces are ignored.
    `with_tabs` : bool
        Whether to treat tabs as ``{TAB}``. If `False`, tabs are ignored.
    `with_newlines` : bool
        Whether to treat newlines as ``{ENTER}``. If `False`, newlines are ignored.
    `turn_off_numlock` : bool
        Whether to turn off `NUMLOCK` before sending keys.

    example::

        SendKeys("+hello{SPACE}+world+1")

    would result in ``"Hello World!"``
    """

    restore_numlock = False
    try:
        # read keystroke keys into a list of 2 tuples [(key,up),]
        _keys = str2keys(keys, with_spaces, with_tabs, with_newlines)

        # certain keystrokes don't seem to behave the same way if NUMLOCK
        # is on (for example, ^+{LEFT}), so turn NUMLOCK off, if it's on
        # and restore its original state when done.
        if turn_off_numlock:
            restore_numlock = toggle_numlock(False)

        # "play" the keys to the active window
        playkeys(_keys, pause)
    finally:
        if restore_numlock and turn_off_numlock:
            key_down(CODES['NUMLOCK'])
            key_up(CODES['NUMLOCK'])
In das hier umändert

Code: Alles auswählen

def playkeys(keys, pause=.05,down=True,up=True):
    """
    Simulates pressing and releasing one or more keys.

    `keys` : str
        A list of 2-tuples consisting of ``(keycode,down)``
        where `down` is `True` when the key is being pressed
        and `False` when it's being released.

        `keys` is returned from `str2keys`.
    `pause` : float
        Number of seconds between releasing a key and pressing the
        next one.
    """
    for (vk, arg) in keys:
        if vk:
            if arg and down is True:
                key_down(vk)
            if up is True:
                key_up(vk)
                if pause:   # pause after key up
                    time.sleep(pause)
        else:
            time.sleep(arg)


def SendKeys(keys,
             down=True,
             up=True,
             pause=0.005,
             with_spaces=True,
             with_tabs=False,
             with_newlines=True,
             turn_off_numlock=True):
    """
    Sends keys to the current window.

    `keys` : str
        A string of keys.
    `pause` : float
        The number of seconds to wait between sending each key
        or key combination.
    `with_spaces` : bool
        Whether to treat spaces as ``{SPACE}``. If `False`, spaces are ignored.
    `with_tabs` : bool
        Whether to treat tabs as ``{TAB}``. If `False`, tabs are ignored.
    `with_newlines` : bool
        Whether to treat newlines as ``{ENTER}``. If `False`, newlines are ignored.
    `turn_off_numlock` : bool
        Whether to turn off `NUMLOCK` before sending keys.

    example::

        SendKeys("+hello{SPACE}+world+!")

    would result in ``"Hello World!"``
    """

    restore_numlock = False
    try:
        # read keystroke keys into a list of 2 tuples [(key,up),]
        _keys = str2keys(keys, with_spaces, with_tabs, with_newlines)

        # certain keystrokes don't seem to behave the same way if NUMLOCK
        # is on (for example, ^+{LEFT}), so turn NUMLOCK off, if it's on
        # and restore its original state when done.
        if turn_off_numlock:
            restore_numlock = toggle_numlock(False)

        # "play" the keys to the active window
        playkeys(_keys, pause,down,up)
    finally:
        if restore_numlock and turn_off_numlock:
            key_down(CODES['NUMLOCK'])
            key_up(CODES['NUMLOCK'])
und man dann den Befehl SendKeys.SendKeys("{LWIN}",up=False)
eingibt drückt er die linke WinTaste runter.
Das kann man rückgänig man die Taste nocheinmal drückt oder indem man den Befehl
SendKeys.SendKeys("{LWIN}",down=False)
oder
SendKeys.SendKeys("{LWIN}")
eingibt.
Zuletzt geändert von INFACT am Samstag 7. Februar 2009, 16:57, insgesamt 1-mal geändert.
Benutzeravatar
str1442
User
Beiträge: 520
Registriert: Samstag 31. Mai 2008, 21:13

Man überprüft Wahrheitswerte (und "Werte" an sich eigentlich auch nicht) nicht mit "is", das überprüft auf Objektidentität (Also ob das Objekt 1 und Objekt 2 das selbe sind, nicht ob sie nur gleichwertig sind) , die natürlich nicht gegeben sein muss.
derdon
User
Beiträge: 1316
Registriert: Freitag 24. Oktober 2008, 14:32

...sondern so (weil str1442 nur gesagt hat, wie man es nicht macht):

Code: Alles auswählen

if foo:
    print 'foo is true!'
elif not foo: # or else:
    print 'foo is false!'
DasIch
User
Beiträge: 2718
Registriert: Montag 19. Mai 2008, 04:21
Wohnort: Berlin

Mir fällt da gerade in Zeile 18

Code: Alles auswählen

if arg and down is True:
auf. Das funktioniert zwar aber ich vermute du weisst nicht wieso.

"arg and down is True" prüft nicht ob "arg is True" und "down is True" gilt sondern es prüft ob "arg" und "down is True" gilt. arg evaluiert sich zu True wenn es nicht False, None, eine Sequenz der Länge 0 oder ein anderes Objekt dessen __nonzero__ Methode False zurückgibt ist.
INFACT
User
Beiträge: 385
Registriert: Freitag 5. Dezember 2008, 16:08

Ja das weiß ich, ich hab das ja auch nicht vom ehemaligen Code geändert :K :mrgreen:
[b][i]ein kleines game für die die lust haben http://konaminut.mybrute.com[/i][/b]
;-)
Benutzeravatar
Hyperion
Moderator
Beiträge: 7478
Registriert: Freitag 4. August 2006, 14:56
Wohnort: Hamburg
Kontaktdaten:

Wäre das nicht besser bei den Snippets aufgehoben als hier bei Links und Tutorials?
tbjhdaniele
User
Beiträge: 1
Registriert: Mittwoch 3. November 2010, 17:01

Ist es zu "easy" key_up und key_down zu benutzen ?
Antworten