As I promised couple of day a go

Gandalf goldnery at gmail.com
Thu May 29 20:37:53 EDT 2008


When we discuss a  global window events. and i wanted to generate
event when the user click on a key out of my application. I promised
that if will found how to do it i'l show you.

I found this article which explain very simply how to hook py width
pyHook lib
http://mindtrove.info/articles/monitoring-global-input-with-pyhook/

now the following code combine between wxpython and phHook. I know
this code probably looks not efficient and unorganized. I'm really new
width this stuff, but it works fine.

before you try it you better install the pyHook from the following
link
http://mindtrove.info/articles/monitoring-global-input-with-pyhook/
*note theirs an image url you would have to edit in order for it to be
work

#!/usr/bin/python

# menu1.py

import wx
import wx.html as html
import pythoncom, pyHook


class MyFrame(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition,
wx.Size(400, 400))
        self.SetBackgroundColour('#fef9d8')

        panel= wx.Panel(self, -1, style=wx.SIMPLE_BORDER, size=(400,
100));
        aaa= html.HtmlWindow(panel, -1, style=wx.SIMPLE_BORDER,
size=(250, 60))
        aaa.LoadPage('aa.html')
        cnrl=wx.TextCtrl(aaa, 11, '', (20, 20), (120, 20))
        wx.Button(aaa, 12, "Translate", (160, 19))
        self.Bind(wx.EVT_BUTTON, self.OnClick, id=12)
        quite = wx.BitmapButton(panel, 20, wx.Bitmap('py/x.gif'),(370,
0), (20, 20), style=wx.ALIGN_RIGHT )
        self.Bind(wx.EVT_BUTTON, self.Yehuda, id=20)
        self.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
        self.SetFocus()
        font1= wx.Font(12, wx.NORMAL, wx.NORMAL, wx.BOLD)
        font2=wx.Font(10, wx.NORMAL, wx.NORMAL, wx.NORMAL)
        text1=wx.StaticText(panel, 15, "Hello" ,(30, 70) ,
style=wx.ALIGN_CENTRE)
        text1.SetFont(font1)
        text2=wx.StaticText(panel, -1, "Peace, By, Good by", (30,
100), style=wx.ALIGN_CENTRE )
        text2.SetFont(font2)
        self.Show()


        self.Centre()

    def Yehuda(self, event):
        self.Hide()
    def OnKeyDown(self, event):
        keycode = event.GetKeyCode()
        if keycode == wx.WXK_ESCAPE:
            ret  = wx.MessageBox('Are you sure to quit?', 'Question',
		wx.YES_NO | wx.NO_DEFAULT, self)
            if ret == wx.YES:
                self.Close()
        event.Skip()
    def OnClick(self,event):
        text1.SetLabel(cnrl.GetValue())

    def OnPaint(self, event):
        dc = wx.PaintDC(frame)
        dc.DrawBitmap(frame.bitmap, 20, 20)



class MyTaskBarIcon(wx.TaskBarIcon):
    def __init__(self, frame):
        wx.TaskBarIcon.__init__(self)

        self.frame = frame

        self.SetIcon(wx.Icon('menu1.png', wx.BITMAP_TYPE_PNG))
        self.Bind(wx.EVT_MENU, self.OnTaskBarActivate, id=1)
        self.Bind(wx.EVT_MENU, self.OnTaskBarDeactivate, id=2)
        self.Bind(wx.EVT_MENU, self.OnTaskBarClose, id=3)
        #self.frame.Bind(wx.EVT_CLOSE, self.OnClose)

    def CreatePopupMenu(self):
        menu = wx.Menu()
        menu.Append(1, 'Show')
        menu.Append(2, 'Hide')
        menu.Append(3, 'Close')
        return menu

    def OnTaskBarClose(self, event):
        self.frame.Close()

    def OnTaskBarActivate(self, event):
        if not self.frame.IsShown():
            self.frame.Show()

    def OnTaskBarDeactivate(self, event):
        if self.frame.IsShown():
            self.frame.Hide()

def OnKeyboardEvent(event):
        global x, frame, frame2
        if event.KeyID ==
162:
            if x==1:
               frame.Close()
               frame2= MyFrame(None, -1,  'frame')
               x=0
            else:
               frame2.Close()
               frame= MyFrame(None, -1,  'frame2')
               x=1

x=1
app = wx.App()
frame = MyFrame(None, -1,  'Babylon')
frame.tskic = MyTaskBarIcon(frame)
frame.Show(True)
app.SetTopWindow(frame)
hm = pyHook.HookManager()
hm.KeyDown = OnKeyboardEvent
hm.HookKeyboard()
pythoncom.PumpMessages()
app.MainLoop()






More information about the Python-list mailing list