pan cla”

Posted on

Question :

Answer #2:

If you’re using Python for Windows then there’s a very good chance that you have the win32api module, which handles hooking into the API for you…

import win32api
import win32con
win32api.keybd_event(win32con.SHIFT_PRESSED, 0,win32con.KEYEVENTF_EXTENDEDKEY, 0)

Does that help? (p.s. you should install IPython if, at all possible, it’s massively helpful for experimenting)

Answered By: Wayne Werner

Answer #3:

I know that this question is really old, but I found a nice library: PyUserInput.
I am not sure how to download it in windows(I haven’t downloaded libraries for windows for a long time). In Linux is easy just download pip(if you haven’t):sudo apt-get install pip and then sudo pip install pyuserinput.
And then the code is realy simple:

#imports the library
from pykeyboard import PyKeyboard
#import the sleep function
from time import sleep
#initialize the keyboard simulator
keyboard = PyKeyboard()
#presses the key
keyboard.press_key('x')
#waits five seconds before releasing the key
sleep(5)
#releases the key
keyboard.release_key('x')
Answered By: FreakyOne

Answer #4:

I’ve modified the Noctis answer to work with Unicode characters.

“Simple unicode keyboard automation for windows”
https://gist.github.com/ubershmekel/4b414a66037feaea595b5f4e78220aad

The above link has a gist, this is the code from that gist today:

# coding: utf-8
"""
Simple unicode keyboard automation for windows
Based off of http://stackoverflow.com/questions/11906925/python-simulate-keydown
"""

import ctypes
import time
import sys

LONG = ctypes.c_long
DWORD = ctypes.c_ulong
ULONG_PTR = ctypes.POINTER(DWORD)
WORD = ctypes.c_ushort

INPUT_MOUSE = 0
INPUT_KEYBOARD = 1
INPUT_HARDWARE = 2

KEYEVENTF_EXTENDEDKEY = 0x0001
KEYEVENTF_KEYUP = 0x0002
KEYEVENTF_SCANCODE = 0x0008
KEYEVENTF_UNICODE = 0x0004


class MOUSEINPUT(ctypes.Structure):
    _fields_ = (('dx', LONG),
                ('dy', LONG),
                ('mouseData', DWORD),
                ('dwFlags', DWORD),
                ('time', DWORD),
                ('dwExtraInfo', ULONG_PTR))


class KEYBDINPUT(ctypes.Structure):
    _fields_ = (('wVk', WORD),
                ('wScan', WORD),
                ('dwFlags', DWORD),
                ('time', DWORD),
                ('dwExtraInfo', ULONG_PTR))


class HARDWAREINPUT(ctypes.Structure):
    _fields_ = (('uMsg', DWORD),
                ('wParamL', WORD),
                ('wParamH', WORD))


class _INPUTunion(ctypes.Union):
    _fields_ = (('mi', MOUSEINPUT),
                ('ki', KEYBDINPUT),
                ('hi', HARDWAREINPUT))


class INPUT(ctypes.Structure):
    _fields_ = (('type', DWORD),
                ('union', _INPUTunion))


def send_input(*inputs):
    nInputs = len(inputs)
    LPINPUT = INPUT * nInputs
    pInputs = LPINPUT(*inputs)
    cbSize = ctypes.c_int(ctypes.sizeof(INPUT))
    return ctypes.windll.user32.SendInput(nInputs, pInputs, cbSize)


def input_structure(structure):
    if isinstance(structure, MOUSEINPUT):
        return INPUT(INPUT_MOUSE, _INPUTunion(mi=structure))
    if isinstance(structure, KEYBDINPUT):
        return INPUT(INPUT_KEYBOARD, _INPUTunion(ki=structure))
    if isinstance(structure, HARDWAREINPUT):
        return INPUT(INPUT_HARDWARE, _INPUTunion(hi=structure))
    raise TypeError('Cannot create INPUT structure!')


def keyboard_input(code, flags):
    return KEYBDINPUT(0, code, flags, 0, None)


def keyboard_event(code, flags=KEYEVENTF_UNICODE):
    return input_structure(keyboard_input(code, flags))


def press(character):
    code = ord(character)
    send_input(keyboard_event(code))
    send_input(keyboard_event(code, KEYEVENTF_KEYUP))


def main():
    time.sleep(3)
    for char in u'On??2E6UXo??2E^uXh#:SHn&HQ':
        press(char)
        time.sleep(0.5)

if __name__ == '__main__':
    main()
Answered By: ubershmekel

Answer #5:

You can use pynput library which is easy to use.

from pynput.keyboard import Key, Listener, Controller as keyboard_controller
keyboard = keyboard_controller()

keyboard.type("Please enter your desired key's code (HEX): ")

You can also simulate mouse even with this library.

Answered By: Jihun

Leave a Reply

Your email address will not be published.