Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8909283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:23:27+00:00 2026-06-15T03:23:27+00:00

short summary: I am trying to create a program that will send keyboard events

  • 0

short summary:

I am trying to create a program that will send keyboard events to the computer that for all purposes the simulated events should be treated as actual keystrokes on the keyboard.

original post:

I am looking for a way to generate keyboard events using python.

Assume that the function receives a key that it must simulate pressing, like so:

keyboardevent('a') #lower case 'a'
keyboardevent('B') #upper case 'B'
keyboardevent('->') # right arrow key

def keyboardevent(key):
    #code that simulated 'key' being pressed on keyboard

The above are obviously examples, but what I am looking for is a library, module, or whatever, which I can use to simulate keyboard events.

note: This is different than sending characters to notepads, or inputting text into fields or such. I want the python script to simulate an actual keyboard event, the computer will think that there is really a keyboard event.

Extra Note:

I don’t want to send keystrokes to the active window – I want the system to believe the keyboard’s keys are being pressed, subtle difference, as some active-windows do not accept certain key-combinations, or if I wanted to use keyboard shortcuts for background processes through my script, they don’t need to go through the active-window

So far I have looked at these things:

Generate keyboard events for the frontmost application

How to generate keyboard keypress events through Python?

Which were all about apple and didn’t help at all.

And this:

Which is the easiest way to simulate keyboard and mouse on Python?

Which seems like it might be what I need, but I can not find the library for it or any documentation.

I have searched more places as well, but have yet to find a solution.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T03:23:28+00:00Added an answer on June 15, 2026 at 3:23 am

    It can be done using ctypes:

    import ctypes
    from ctypes import wintypes
    import time
    
    user32 = ctypes.WinDLL('user32', use_last_error=True)
    
    INPUT_MOUSE    = 0
    INPUT_KEYBOARD = 1
    INPUT_HARDWARE = 2
    
    KEYEVENTF_EXTENDEDKEY = 0x0001
    KEYEVENTF_KEYUP       = 0x0002
    KEYEVENTF_UNICODE     = 0x0004
    KEYEVENTF_SCANCODE    = 0x0008
    
    MAPVK_VK_TO_VSC = 0
    
    # msdn.microsoft.com/en-us/library/dd375731
    VK_TAB  = 0x09
    VK_MENU = 0x12
    
    # C struct definitions
    
    wintypes.ULONG_PTR = wintypes.WPARAM
    
    class MOUSEINPUT(ctypes.Structure):
        _fields_ = (("dx",          wintypes.LONG),
                    ("dy",          wintypes.LONG),
                    ("mouseData",   wintypes.DWORD),
                    ("dwFlags",     wintypes.DWORD),
                    ("time",        wintypes.DWORD),
                    ("dwExtraInfo", wintypes.ULONG_PTR))
    
    class KEYBDINPUT(ctypes.Structure):
        _fields_ = (("wVk",         wintypes.WORD),
                    ("wScan",       wintypes.WORD),
                    ("dwFlags",     wintypes.DWORD),
                    ("time",        wintypes.DWORD),
                    ("dwExtraInfo", wintypes.ULONG_PTR))
    
        def __init__(self, *args, **kwds):
            super(KEYBDINPUT, self).__init__(*args, **kwds)
            # some programs use the scan code even if KEYEVENTF_SCANCODE
            # isn't set in dwFflags, so attempt to map the correct code.
            if not self.dwFlags & KEYEVENTF_UNICODE:
                self.wScan = user32.MapVirtualKeyExW(self.wVk,
                                                     MAPVK_VK_TO_VSC, 0)
    
    class HARDWAREINPUT(ctypes.Structure):
        _fields_ = (("uMsg",    wintypes.DWORD),
                    ("wParamL", wintypes.WORD),
                    ("wParamH", wintypes.WORD))
    
    class INPUT(ctypes.Structure):
        class _INPUT(ctypes.Union):
            _fields_ = (("ki", KEYBDINPUT),
                        ("mi", MOUSEINPUT),
                        ("hi", HARDWAREINPUT))
        _anonymous_ = ("_input",)
        _fields_ = (("type",   wintypes.DWORD),
                    ("_input", _INPUT))
    
    LPINPUT = ctypes.POINTER(INPUT)
    
    def _check_count(result, func, args):
        if result == 0:
            raise ctypes.WinError(ctypes.get_last_error())
        return args
    
    user32.SendInput.errcheck = _check_count
    user32.SendInput.argtypes = (wintypes.UINT, # nInputs
                                 LPINPUT,       # pInputs
                                 ctypes.c_int)  # cbSize
    
    # Functions
    
    def PressKey(hexKeyCode):
        x = INPUT(type=INPUT_KEYBOARD,
                  ki=KEYBDINPUT(wVk=hexKeyCode))
        user32.SendInput(1, ctypes.byref(x), ctypes.sizeof(x))
    
    def ReleaseKey(hexKeyCode):
        x = INPUT(type=INPUT_KEYBOARD,
                  ki=KEYBDINPUT(wVk=hexKeyCode,
                                dwFlags=KEYEVENTF_KEYUP))
        user32.SendInput(1, ctypes.byref(x), ctypes.sizeof(x))
    
    def AltTab():
        """Press Alt+Tab and hold Alt key for 2 seconds
        in order to see the overlay.
        """
        PressKey(VK_MENU)   # Alt
        PressKey(VK_TAB)    # Tab
        ReleaseKey(VK_TAB)  # Tab~
        time.sleep(2)
        ReleaseKey(VK_MENU) # Alt~
    
    if __name__ == "__main__":
        AltTab()
    

    hexKeyCode is the virtual keyboard mapping as defined by the Windows API. The list of codes is available on MSDN: Virtual-Key Codes (Windows)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a small Powershell function that will return some summary data
I'm attempting to create a sort of 'summary' view, where values should be pulled
I have an XML Schema that I'm trying to create C# classes with. At
A short summary of the question: I have a parent class which is extended
Summary Long story short, we've been tasked with gutting the authentication and authorization parts
Short one: Am I correct in assuming, that the Samsung Galaxy S wifi 4.0
Here is the summary: I need to build a simple java web app that,
I'm trying to write a buffermanager that manages 3 Streams. The typical usage would
I want to create an autogenerated proxy for WCF service that allows me to
Short, at-a-glance summary of question (TLDR): how do I ensure a Java Object is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.