I am learning both autohotkey and python. I wrote this script in ahk (and it works!) that automatically joins tables (using tableninja) in the pokerstars client--
^q::
Loop
{
Send q
Sleep 500
Send {PgUp}
Sleep 500
Send w
Sleep 60000
}
return
I'd like to convert this into python--could you give me an idea as to which modules I can use to accomplish this?
What the python script needs to do is to (while looping) type in a letter (on a notepad that's already open), go down two lines, type in another letter, then wait one minute before starting over.
I am thinking--
import module to auto-type letters
import module that works as timer
def function
type letter q
enter
enter
def function
type letter w
def function
sleep
while True
function
function
function
I am teaching myself how to code. I haven't reached that part about python modules just yet. Thanks!
Assuming you work on windows(don't think AHK runs on anything else), you should check out sendkeys. It will make sending keystrokes a piece of cake. If you want somthing a little more robust, take a look at pywinauto
For the shortcut part, take a look at pyhook
I suggest these modules:
SendKeysCtypes for any sending of keystrokes and sending shortcuts to a window.
SendKeysCtypes is a new and more stable version of SendKeys. I have had issues with SendKeys in the past.
PYHK to deal with global hotkeys - receive hotkeys and trigger functions.
PYHK is based on pyHook and makes hotkey registration very simple. I wrote it because I had the exact same idea as you - I wanted to to do AHK functionality in python.
win32gui for window handling such as moving resizing.
I personally prefer win32gui for short, simple tasks. I use pywinauto for more complex tasks. An example would be if I had to access a menu within a program (like File-New).
mouse.py to control the mouse. This is the most robust way I have found so far. The version I use is an extension of a module I found here at stackoverflow - ctypes mouse_events.
I have personally done several programs for poker with python. I have released source code of my smaller programs. You can find them with source on my website schurpf.com/poker-software.
there's also AutoPy, a cross-platform library for this purpose.
Related
I'm a comp sci student studying some compiler design and I have a quick question that bugs me to no end.
I'm currently writing an Interpreter in JavaScript (run on nodeJS) that takes statements like:
x = 4
print x
Which would result in the console output:
4
I can parse these statements pretty easily and have them output stuff to the console window. But how would this process work with GUI applications?
The way I understand it is, let's take Python for example.
When you run python in the command line, it launches a console application which takes in python commands and interprets them. (I know it translates to bytecode first, but it does eventually get interpreted).
So like if it sees 1+1, I understand how it can parse this and return 2 to the console window that it is already running. Python in this case is itself is a console app, so it's intuitive that console output from user-inputted instructions can also be on the console.
I've been able to do THAT. But this ALSO works in python:
from tkinter import*
t = Tk()
How does THAT work? Does the Python Interpreter/VM somehow call a Win32 API function? Or does it draw its own window?
Thank you in advance, for any help given to clarify this.
tkinter is essentially just a Python interface to the Tk library. This is an open source library that runs on all popular operating systems and provides a consistent API for GUI elements.
I don't think Python has any built-in GUI functions, it relies on external libraries like this.
Somebody at some point long ago wrote a library that could directly access the screen and turn pixels on or off. Then, they wrote a function that takes two x,y pairs and computes all the pixels that need to be turned on to draw a line. The library would then call the function to turn pixels on or off.
Someone else then created a library that sits on top of that library, and uses it to not just draw lines, but draw boxes or circles and so on.
Someone else creates a library on top of that which can read font descriptions and turn that into text on the screen. And then someone creates a library that combines the font library and the line library to create a library that creates windows and checkbuttons. And then someone figures out how to add color, and object rotation, and 3d effects, and on and on.
Ultimate we end up with something like tkinter, which is a library that has functions for creating an input widget, which calls a tcl/tk library which converts the python to tcl, and which calls an X11 or DirectX or Win32 or Cocoa or OpenGL library which takes the input and calls some other function that ultimately turns a pixel on or off on the physical display.
When you deal with programming, A LOT of what you are able to do comes down to existing libraries and APIs. If you had to reinvent the wheel every time, you'd never get anything meaningful done.
A simpler example is your print() call. This is mearly a wrapper that writes to stdout. The bash shell / OS you're using handles what happens to stdout. GUIs are more or less the same thing, with just a slightly more complicated path.
tkinter is a python library for generating GUI interfaces. It itself, is nothing more than a wrapper for the more general, Tk library. Tk is a general purpose GUI library that works across platforms. It does this by creating utilizing code that's customized for each operating system's GUI library. It's the OS* itself that ends up creating the GUI.
*This is kind of a generalization as in some operating systems (such as those that utilize something such as Gnome) the GUI interface is more decoupled from the OS than one would often think.
I'm creating an application to automatically perform keystrokes into the same, multiple windows that are open at the same time. These windows are all child windows from a parent.
For example, there are four windows and I want to press a key that will send CTRL-K to them, then immediately send WIN+M to all of them. After some time, I want to press another key that will then send SHIFT+WIN+M, and then CTRL+O.
I'm stuck on capturing windows using EnumWindow and all of that. I'm familiar with callbacks, but a little confused on how to use them.
I'm also considering using pyWinAuto's SendKeys.
I do realize there are programs like X-Mouse Button Control that can do all of this for me, but I'm learning and this would be a fun exercise.
I would recommend using PyAutoit for this, it's fairly straightforward to use and it usually works well. Steps to install the package and a simple example can be found here :
https://pypi.python.org/pypi/PyAutoIt/0.3
I'm trying to write a program than will detect when my mouse pointer will change icon and automatically send out a mouse click. Is there a better way to do this than to take screenshots and parse the image for the mouse icon?
EDIT:
I'm running my program on windows 7.
I'm trying to learn some image processing and make a simple flash game i made automated.
Rules: when the curses changes shape, click to get a point.
Also what imaging modules for python will allow you to take a specific size screenshot not just the whole screen? This question has moved to a new thread: "Taking Screen shots of specific size"
The way to do this in Windows is to install either a global message hook with SetWindowsHookEx or SetWinEventHook. (Alternatively, you could build a DLL that embeds Python and hooks into the browser or its Flash wrapper app and do it less intrusively from within the app, but that's much more work.)
The message you want is WM_SETCURSOR. Note that this is the message sent by Windows to the app to ask whether it wants to change the cursor, not a message sent when the cursor changes. So, IIRC, you will want to put a WH_CALLWNDPROC and a WH_CALLWNDPROCRET and check GetCursorInfo before and after to see if the app has done so.
So, how do you do this from Python? Honestly, if you don't already know both win32api and friends from the pywin32 package, and how to write Windows message procs in some language, you probably don't want to. If you do want to, I'd start off with the (abandoned) pyHook project from UNC Assist. Even if you can't get it working, it's full of useful source code.
You should also search SO for [python] SetWinEventHook and [python] SetWindowsHookEx, and google around a bit; there are some examples out there (I even wrote one here somewhere…)
You can look at higher-level wrapper frameworks like pywinauto and winGuiAuto, but as far as I know, none of them has much help for capturing events.
I believe there are other tools, maybe AutoIt, that have all the functionality you need, but not in Python module. (AutoIt, for example, has its own VB-like scripting language instead.)
I saw here a solution, but i don't want wait until the key is pressed. I want to get the last key pressed.
The related question may help you, as #S.Lott mentioned: Detect in python which keys are pressed
I am writting in, though to give yu advice: don't worry about that.
What kind of program are you trying to produce?
Programas running on a terminal usually don't have an interface in which getting "live" keystrokes is interesting. Not nowadays. For programs running in the terminal, you should worry about a usefull command line User Interfase, using the optparse or other modules.
For interative programs, you should use a GUI library and create a decent UI for your users, instead of reinventing the wheel.Which wouldb eb etter for what you ar trying to do? Theuser click on an icon,a window opens on the screen, witha couple of buttons on it, and half a dozen or so menu options packed under a "File" menu as all the otehr windws on the screen - or - a black terminal opens up, with an 80's looking text interface with some blue-highlighted menu options and so on?. You can use Tkinter for simple windowed applications, as it comes pre-installed with Python + Windows, so that yoru users don't have to worry about installign aditional libraries.
Rephrasing it just to be clear: Any program that requires a user interface should either se a GUI library, or have a WEB interface. It is a waste of your time, and that of your users, to try and create a UI operating over the terminal - we are not in 1989 any more.
If you absolutely need a text interface, you should look at the ncurses library then. Better than trying to reinvent the wheel.
http://code.activestate.com/recipes/134892/
i think it's what you need
ps ooops, i didn't see it's the same solution you rejected...why, btw?
edit:
do you know:
from msvcrt import getch
it works only in windows, however...
(and it is generalised in the above link)
from here: http://www.daniweb.com/forums/thread115282.html
Using Perl, Python, or Ruby, can I write a program, probably calling Win32 API, to "click" on the screen at scheduled time, like every 1 hour?
Details:
This is for experimentation -- and can the clicking be effective on Flash content as well as any element on screen? It can be nice if the program can record where on screen the click needs to happen, or at least draw a red dot on the screen to show where it is clicking on.
Can the click be targeted towards a window or is it only a general pixel on the screen? What if some virus scanning program pops up covering up the place where the click should happen? (although if the program clicks on the white space of a window first, then it can bring that window to the foreground first).
By the way, can Grease Monkey or any Firefox add-on be used to do this too?
If you are trying to automate some task in a website you might want to look at WWW::Selenium. It, along with Selenium Remote Control, allows you to remote control a web browser.
In Python there is ctypes and in Perl there is Win32::API
ctypes Example
from ctypes import *
windll.user32.MessageBoxA(None, "Hey MessageBox", "ctypes", 0);
Win32::Api Example
use Win32::GUI qw( WM_CLOSE );
my $tray = Win32::GUI::FindWindow("WindowISearchFor","WindowISearchFor");
Win32::GUI::SendMessage($tray,WM_CLOSE,0,0);
To answer the actual question, in Perl, you would use the SendMouse (and the associated functions) provided by the Win32::GuiTest module.
#!/usr/bin/perl
use strict;
use warnings;
use Win32::GuiTest qw( MouseMoveAbsPix SendMouse );
MouseMoveAbsPix(640,400);
SendMouse "{LEFTCLICK}";
__END__
UPDATE:
What if some virus scanning program pops up covering up the place
where the click should happen?
In that case, you would use FindWindowLike to find the window and MouseClick to send a click to that specific window.
If using a different tool is allowed, you should take a look at AutoHotkey or AutoIt. These tools were made for this sort of thing, and I've always been keen on using the right tools for the right jobs.
AutoHotkey is based on AutoIt I believe, and it is my personal preference. You only really need 2 functions for what you're trying to achieve, MouseMove and MouseClick.
I find this is easier to approach in Java or C++. Java has a Robot class that allows you to just pass x, y coordinates and click somewhere. Using C++, you can achieve that same functionality using mouse_event() or SendMessage() with the WM_MOUSE_DOWN flag. SendMessage is more technical but it allows you to use FindWindow() and send mouse clicks to a specific window, even if it's minimized.
Using a scripting language like Python or Ruby, I'd guess that you'd end up hooking into one of these Windows API functions anyway.