Keydown Event and Focus Problem

Discussion in 'Python' started by mee-shell, Oct 23, 2004.

  1. mee-shell

    mee-shell Guest

    Hi, I am a python newbie. I have created a tool for capturing the
    screen shot. I want to be able to do the screen capture with the
    keydown event as well. The tool worked fine when it first opened.
    However, if I keep the tool open, and open another window to do the
    screen capture, the tool will lose its focus, and the keydown event
    will not work anymore. I wonder if any of you can help me to solve
    this problem of how to get the focus back to the tool after opening a
    new window? Here is my code, and thank you in advance.


    from wxPython.wx import *
    import Image
    import ImageGrab
    import time, string, os
    TITLE = "wxPython test"

    class MyFrame(wxFrame):
    def __init__(self, parent, ID, title):
    wxFrame.__init__(self, parent, ID, title,
    wxDefaultPosition,
    wxSize(400, 25), style = wxSIMPLE_BORDER|wxSTAY_ON_TOP)

    #------------------------------------------------------------------
    # add a panel
    self.panelCNB = wxPanel(self, -1, wxPoint(15,0),
    wxSize(200,25))
    self.panelCNB.SetOwnBackgroundColour(wxColor(99,148,214))
    screenButton = wxButton(self.panelCNB, 1000, 'screen shot')
    exitButton = wxButton(self.panelCNB, 5000, 'exit')

    buttonSizer = wxBoxSizer(wxHORIZONTAL)
    buttonSizer.Add(screenButton, 0)
    buttonSizer.Add(exitButton, 0)
    self.panelCNB.SetSizer(buttonSizer)
    self.panelCNB.Layout()

    # Set the focus so that the panel gets key events
    self.SetFocus()

    # button event and key down
    EVT_BUTTON(screenButton, 1000, self.ScreenShot)
    EVT_BUTTON(exitButton, 5000, self.Exit)
    EVT_KEY_DOWN(self, self.OnKeyDown)

    def Exit(self, evt):
    self.Close(true)

    def ScreenShot(self, evt):
    filename = time.strftime("%Y%m%d%H%M%S", time.localtime())
    temp = filename + 'temp.jpg'
    thumbnail = filename + 'thumb.jpg'
    file = filename + '.jpg'

    ImageGrab.grab().save(temp)
    testimage=Image.open(temp)
    testimage.thumbnail((800,600),Image.ANTIALIAS)
    testimage.save(file)
    testimage.thumbnail((100,75),Image.ANTIALIAS)
    testimage.save(thumbnail)

    def OnKeyDown(self, evt):
    if evt.ControlDown() and evt.GetKeyCode() == ord('R'):
    self.SetFocus()
    self.ScreenShot(evt)

    app = wxPySimpleApp()
    frame = MyFrame(None, -1, TITLE)
    frame.SetBackgroundColour(wxColor(99,148,214))
    frame.Show(true)
    app.MainLoop()
     
    mee-shell, Oct 23, 2004
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Claudia Fong

    keydown event

    Claudia Fong, Sep 17, 2007, in forum: ASP .Net
    Replies:
    5
    Views:
    1,965
    Claudia Fong
    Sep 18, 2007
  2. Replies:
    1
    Views:
    669
    cfps.Christian
    May 23, 2008
  3. jaspal singh

    trap keypress and keydown event in asp.net mobile form

    jaspal singh, Nov 24, 2006, in forum: ASP .Net Mobile
    Replies:
    1
    Views:
    1,438
  4. Atul Thombre

    KeyDown event on Smartphones

    Atul Thombre, Sep 11, 2008, in forum: ASP .Net Mobile
    Replies:
    0
    Views:
    839
    Atul Thombre
    Sep 11, 2008
  5. Gregor Kofler
    Replies:
    6
    Views:
    149
    dhtml
    Oct 30, 2008
Loading...

Share This Page