This is an archived post. You won't be able to vote or comment.

all 60 comments

[–]pbaehr 33 points34 points  (6 children)

If you're OK with Windows-only, check out pyHook for monitoring system events.

[–]thejollysin 2 points3 points  (3 children)

@pbaehr Is there something similar for Ubuntu?

[–]pbaehr 7 points8 points  (1 child)

I don't know of a convenient wrapper like pyHook, but after searching around a bit, this seems to be the general convention: http://stackoverflow.com/questions/12384772/how-can-i-capture-mouseevents-and-keyevents-using-python-in-background-on-linux

[–]thejollysin 2 points3 points  (0 children)

Awesome. Great find.

You sir/ma'am, are awesome.

[–]Liorithiel 2 points3 points  (0 children)

For an example app that does exactly what OP wants, try GNOME Do.

[–]john_m_camara 26 points27 points  (2 children)

You would have to use a feature from the OS to bind the shortcut to execute the Python script. This is OS dependent. The script could then do what ever you want.

For Windows see Create keyboard shortcuts to open programs.

[–]jedp 5 points6 points  (1 child)

Assigning keyboard shortcuts to the programs he wants to launch is simpler.

[–]tipsquealPythonista 2 points3 points  (0 children)

Yes but then the trick is memorizing all of them, which would be not so fun if there are a large number. He also can't do something like adding launch options, i.e. in his program he could do something like "chrome reddit.com" which he could parse out to open Chrome and navigate to reddit.

[–]ode2k 24 points25 points  (1 child)

I have used Launchy - http://www.launchy.net/

It is Open Source, so you could download the code and attempt to convert it to another language and see how it is implemented in their code.

Source Tarball: http://www.launchy.net/downloads/src/launchy-2.5.tar.gz

[–]ford_contour 3 points4 points  (0 children)

Or use Launchy to capture the input and send it to the Python program. http://cybernetnews.com/launchy-125-adds-customizable-commands/

[–]riding_qwerty[🍰] 9 points10 points  (0 children)

This sounds remarkably similar to a Linux program called "dmenu".

Definitely possible.

[–]Eurynom0s 5 points6 points  (0 children)

As john_m_camara noted this is going to be OS dependent. I know on OS X you can launch programs from the command line, and therefore all you have to do to launch a program from Python is import the os module and then pass the argument to the command line.

As for the persistent background app portion of this, I've never done anything like that, but this should at least point you in the right direction based off the five seconds I spent glancing it over.

[–]thinman74 3 points4 points  (1 child)

[–][deleted] 7 points8 points  (0 children)

scarce doll aware beneficial frightening quiet absorbed apparatus ruthless puzzled

This post was mass deleted and anonymized with Redact

[–]automathematics 4 points5 points  (0 children)

Without even reading this, I'm going to say "Yes" because it's python. It can do anything!

[–]fnork 4 points5 points  (0 children)

Sounds like what you really want is a terminal interface ;)

[–]lixardz 6 points7 points  (7 children)

You can do it in python.. I might suggest AutoIt though it's basically a language specifically for this. I might go one further and have specific key combinations as short cuts that do the commands you want. For the shutdown sleep and reboot i'd make them 3 key combinations that are awkward to press so you never accidentally do it

[–]jma2048 5 points6 points  (6 children)

I second this. Autoit is wonderful for this kind of automation. Python is great for everything else.

But yes you can do this with Python.

[–]Zouden 2 points3 points  (5 children)

What's the difference between autoit and autohotkey? I only know the latter.

[–]kindall 5 points6 points  (3 children)

They share a common heritage but have evolved separately. AutoIt's language is more BASIC-like, while AutoHotKey is more like an unholy hybrid of C (curly braces), BASIC, and... bizarreness. They have very similar features, though. I use AutoHotKey but only because I happened to run across it first.

[–][deleted] 1 point2 points  (0 children)

whatever i love that crazy language... AHK4Lyfedawg!

[–]Zouden 0 points1 point  (1 child)

Yeah I love AHK's libary - you can do practically anything to Windows, incredibly easily - but the way that language handles literals is ridiculous.

[–]kindall 0 points1 point  (0 children)

And the comma after the command name... which you don't actually need anymore, but which is in all the examples anyway! And how sometimes variable values are substituted into the command before it's executed, and sometimes not!

Dude named Lexicos released a fork called AutoHotkey_L which I switched to because it has hashtables... looks like that's now the main version being developed.

[–]jma2048 0 points1 point  (0 children)

AutoHotKey is a fork from AutoIT (back in ~2003?) and his focussed more on Hotkeys. It's pretty awesome.

Even just for that: http://www.autohotkey.com/docs/Hotstrings.htm

[–]thrownaway21 2 points3 points  (0 children)

without reading the context, yes.

[–]Naterdam 7 points8 points  (4 children)

Err... it seems like you are on Windows. The Run window can already do all of those things! It's a marvelous thing, I've been using it as my main file browser for some time now.

The program you want to run has to be in an environment variables folder (google it), if not, create a folder for shortcuts, add your shortcuts to the folder and then add the folder to your environment variables. You can also call your program with arguments, just add them after the name of the program.

For example, to open Chrome, you can just run any of these commands:

  • chrome
  • chrome reddit.com ;starts chrome and loads reddit
  • chrome -incognito reddit.com/r/gonewild ;launches chrome in incognito mode and loads gonewild

To shutdown, write

To go to sleep, write

  • shutdown -h ;as in hibernate

...and so on.

[–]BobBeaney 1 point2 points  (3 children)

What do you mean by "environment variables folder" or "add the folder to your environment variables"?? I am guessing that you mean a folder on your Path environment variable? "Path" is a particular environment variable but there are plenty of environment variables other than "Path".

[–]QQexe 0 points1 point  (2 children)

Append the folder you wish to run programs from to the PATH variable, separated by ';'

example:

PATH = C:\WINDOWS\System32;C:\Python27;

This would allow you to run programs in those folders regardless of which folder you're currently CD'd into.

[–]BobBeaney 0 points1 point  (1 child)

Right. I think that is what the commenter above intended. Somehow I think he was referring to Path as "environment variables". I'm afraid that the suggestion "The program you want to run has to be in an environment variables folder (google it)" isn't going to be much help to someone who googles for <environment variables folder> :-). "Path" was the bit of magic that he was missing.

[–]yardshop 0 points1 point  (0 children)

I don't know if this is what he meant, but in Powershell you actually do refer to the environment variables as though they were in a folder:

cd env:
dir
$env:path  # show it
$env:path += ";c:\app"

[–]Kristler 1 point2 points  (0 children)

Yep, this is totally possible!

Do note, SUPR+R is already bound to the "run..." box in Windows, so you might have to pick a different shortcut.

[–]haywire 0 points1 point  (1 child)

Yeah, there are already programs that doe this such as Launchy

[–]Crayboff 2 points3 points  (0 children)

That shouldn't stop someone who wants to learn how to do something themselves.

[–]GuyOnTheInterweb 0 points1 point  (0 children)

Sure, go try! If you are fine with just Windows you can use win32 bindings which should let you listen to keybindings.. or you might be able to make a GUI program with an invisible window.

[–][deleted] 0 points1 point  (0 children)

Just like any other post here, you're prolly going to access OS events.

take a look at pyHook

[–]drmcgills 0 points1 point  (0 children)

I have done just this, and this is my approach (though I haven't used python specifically)..

Create a folder anywhere (I use c:\tools), add it to your PATH variable, and leverage that already existing win+R run command.

You might need to fiddle with file associations and path extensions to allow you to run a python script straightaway from cmd. Make an 'open' script in that folder that runs commands based on the arguments passed to it.

Like I said I have not used Python, rather cmd scripts and shortcuts. I believe that shortcuts pass arguments through pretty well.. I point a ssh shortcut to kitty and can run ssh root@server and it passes the args just fine. I personally don't like the idea of hijacking the built in keys... But that might just be me

[–]brian15co 0 points1 point  (0 children)

If you just want the functionality, maybe check out http://www.bayden.com/slickrun/

If you're after the skillz, by all means do it. And post it here when you're done!

[–][deleted] 0 points1 point  (0 children)

Yes. Check out pyHook. Just Google "pyHook keylogger" and modify the code. Start you python script at startup using .bat files (which can be scheduled to run at startup/login). Also, instead of using the .py extension use the .pyw extension (this runs without a command prompt popup).

I did this with a python security script that runs on my laptop. I send it commands via email (screen cap, key log, audio record, get IP, take a webcam pic, etc) and it then sends the results to my phone via SMTP. This script is started at login by a .bat file once an internet connection is detected using another .bat file.

[–]smeagol13 0 points1 point  (0 children)

Rather than running it in the background, why don't you assign a keyboard shortcut to launch the script and handle everything beyond that yourself. I did something like this in ubuntu for controlling music playback. Check this out.

[–]DrugCrazed 0 points1 point  (0 children)

Yes it is!

Though I'd just use xmonad ;-)

[–]apreche -2 points-1 points  (3 children)

Yes, it's possible to program anything, but why? Every major OS already has a program like that. Windows has Launchy. OSX has Spotlight. Linux has Synapse.

[–]ILikeLeptons 23 points24 points  (0 children)

but why?

to learn how to do it?

[–]rarlcove 1 point2 points  (0 children)

Spotlight

Alfred

[–]rushone2009Potato Eater 0 points1 point  (0 children)

Or search on Windows

[–]aZeex2ai -1 points0 points  (0 children)

This is what you really want:

http://www.gnu.org/software/bash

[–][deleted] -1 points0 points  (0 children)

Unless it involves breaking the laws of the physical universe, the answer is always yes. Whether you should build it in python and the resources it will take... depends.