0

I am thinking of writing a python program that runs in the background and can inspect user's GUI events.

My requirements is very simple:

1) When user right click the mouse, it can show an option; and when this option is chosen, my program should know this event.

2) When user select a file and click some predefined key combination, my program should know this event.

What should I do? Is this a GUI program? I am also thinking that, this program maybe a daemon on the machine and can inspect the user's GUI event, but I am not sure how can I do this.

Thanks.

ming.kernel
  • 3,005
  • 1
  • 19
  • 29
  • maybe look at http://www.lopesoft.com/en/fmtools/info.html it lets you add custom commands to the context menu – Joran Beasley Aug 15 '12 at 14:06
  • or if you are talking about just your wx program the "wxWidget Inspection Tool" maybe `import wx.lib .inspection; wx.lib .inspection.InspectionTool ().Show () ` http://wiki.wxpython.org/Widget%20Inspection%20Tool – Joran Beasley Aug 15 '12 at 14:10
  • 1
    Do you mean when the user clicks are things that **aren't** inside the wxpython application? I've seen [a similar question](http://stackoverflow.com/questions/10990643/wxpython-set-cursor-to-absolute-screen-location-and-click) but unfortunately the consensus was that this is not possible with wxpython alone. Wxpython can only track things that happen directly to the app, not to other parts of the OS. You will need another library to help track anything outside the app. – acattle Aug 15 '12 at 16:03
  • Thanks @acattle.I means the user clicks are things – ming.kernel Aug 16 '12 at 03:52
  • Thanks @acattle. Yes, I means the user clicks are things aren't inside the wxPython application. I will check your advice. – ming.kernel Aug 16 '12 at 03:53

2 Answers2

2

If you're talking about doing this stuff inside of a wxPython program, then it's all pretty simple. There's a PopupMenu widget for the first one and an AcceratorTable for the second one. If you're wanting to catch mouse and keyboard events outside of a wxPython program, then you have to go very low-level and hook into the OS itself, which means that there really isn't any good way to do it cross-platform. You'll probably want to look at ctypes and similar libraries for that sort of thing.

Mike Driscoll
  • 31,394
  • 6
  • 39
  • 83
  • Thanks, I was looking for method to catch mouse and keyboard events. So you mean that I cannot do this under wxPython? – ming.kernel Aug 16 '12 at 03:50
  • No, you can, but only in the context of your own application. It can catch any mouse or keyboard events you want within it, but it won't catch mouse or keyboard events in another application. – Mike Driscoll Aug 16 '12 at 13:32
  • Thanks, I am planning to divide my task into two parts: 1) a native os program that inspect mouse and keyboard events; 2) a python daemon that receive messages from my native program. Hope this works, I am working on OS X right now. – ming.kernel Aug 17 '12 at 03:03
2

I've been researching this problem a bit and while I don't have a definite answer, I thought it would be good to share what I found.

First, according to the answers for this question, wxPython cannot track nor control the mouse position outside of the area controlled by the wxPython app. However, as we can see in the answers to this question, under certain circumstances it may be possible to create system-wide hot keys that wxPython can see (although we can also see that this is probably not the optimal solution).

So what will work for the OP? Well, build a cross platform application that does what the OP wants seems almost impossible so I will assume the OP is developing for MS Windows. Following the suggestions in those previous two questions, I looked into the PyWin32 API. It's a little hard to find documentation for it but this question offers some helpful links. A little more digging and I found this tutorial for system-wide hotkeys as well as the win32event.WaitForMultipleObjects and win32event.WaitForSingleObject methods which, if the OP can figure out the proper event, may be what the OP needs to catch menu clicks.

I know it's far from a complete answer but hopefully this can serve as a good jumping-off point for further research.

Community
  • 1
  • 1
acattle
  • 2,893
  • 1
  • 14
  • 19