Maybe you know the mmotps GunZ().
i have already written a few macros for it (so a program, which clicks and presses the keys for you ingame) with the program actool(http://www.actool.net/).
But i am tired of using this pascal-like programming language...

So is there a possibility to get a macro, which presses keys and clicks when the user presses a key, written in python?

Dani AI

Generated

Quick answer: yes — Python can do that. The usual pattern on Windows is “listen for a global hotkey / hook, then emit mouse/keyboard events.” For a modern, maintainable approach use a listener library for the hotkey and a separate automation library to send clicks/keys; that keeps the code simple and portable across platforms. See the libraries below for concrete choices and their docs. (pynput.readthedocs.io)

A practical, minimal workflow (listener → action). Example using pynput for the hotkey and PyAutoGUI for actions; swap in PyDirectInput if the game ignores synthetic events:

from pynput import keyboard
import pyautogui

def on_activate():
    # test in Notepad first
    pyautogui.click(200, 200)
    pyautogui.write('macro fired')

with keyboard.GlobalHotKeys({'<alt>+f': on_activate}) as h:
    h.join()

Use the listed docs to expand this into more robust code (debounce, threading, graceful shutdown). (pynput.readthedocs.io)

Notes and troubleshooting (practical gaps from the thread):

  • If a game ignores PyAutoGUI or old mouse_event/keybd_event, try PyDirectInput (it uses SendInput/scan codes). On Windows use SendInput rather than legacy mouse_event; SendInput is subject to UIPI (integrity/elevation), so run at the same privilege level as the game or match integrity when required. (github.com)
  • Grant accessibility/privacy permissions on macOS and test automation in a safe app (Notepad) before targeting a game. Use small delays and a failsafe abort (PyAutoGUI provides PAUSE/FAILSAFE) to avoid runaway scripts. (pyautogui.readthedocs.io)
  • Legal/safety: many online games forbid automated macros and anti-cheat systems can ban accounts; verify the game’s ToS before using macros. (help.steampowered.com)

Context: was right that you need a global hook; modern Python libraries avoid the old pyHook/pascal toolchain and give cleaner APIs. If you prefer the quickest non-Python route, ’s AutoIt (or AutoHotkey) suggestions are still valid for rapid Windows macros.

Recommended Answers

All 5 Replies

So is there a possibility to get a macro, which presses keys and clicks when the user presses a key, written in python?

So, let me see if I have this right. You want a script to emulate mouse clicks/key presses when the user presses a certain key (ALT + F, for example)? If so, then it depends on what OS you're running. I'll assume you're running Windows for now.

This should get you started:

Download pyHook here.
Download win32 extensions (pythoncom) .

#Detecting keystrokes as events
import pyHook
import pythoncom

def OnKeyboardEvent(event):
    print event.Ascii

hm = pyHook.HookManager()
hm.KeyDown = OnKeyboardEvent
hm.HookKeyboard()

while True:
    pythoncom.PumpMessages()

If you are by chance running Linux or another OS with X11 as its windowing system, I can give you one piece of advice: don't do it unless you have to. Just don't. If you're really determined, you can search for hours for some documentation for xbindkeys. If you need help with xbindkeys, post back. I have enough experience with it to help you get started.

Good luck with your scripting.

EDIT: If you want to do things the EASY way, check out AutoHotkey. You could probably write the entire script in about 5 minutes.

Well you can use many languages for autobot program creation.

Python works, but I don't think it is the best way since you have to mess with Win32API and hooks.

I think one of the best ways is to write an Autoit script.

Wow, that works nicely :twisted: :twisted:

but is there a documentation for this module?

and how can i send mouseclicks and keyevents with the win32 module?

It would be nice if u would tell me this too :)

The documentation is pretty much nonexistent, but you can find plenty of examples by searching the web for "python win32 simulate mouse click" or something similar.

Simulate a mouse click:

import win32api
import win32con

def click(x,y):
    win32api.SetCursorPos((x,y))
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, x, y, 0, 0)
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, x, y, 0, 0)

click(100, 100) # simulate mouse click at 100px, 100px

Simulate a key press:

Download SendKeys from .
Then:

from SendKeys import SendKeys
SendKeys('Hello{SPACE}world{ENTER}')

Thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.