Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Keydown Event and Focus Problem

Reply
Thread Tools

Keydown Event and Focus Problem

 
 
mee-shell
Guest
Posts: n/a
 
      10-23-2004
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,14 8,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()
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
keydown listener for div element and "event forwarding" Gregor Kofler Javascript 6 10-30-2008 04:44 AM
KeyDown event on Smartphones Atul Thombre ASP .Net Mobile 0 09-11-2008 12:42 PM
Keydown event problem when enter key is pressed Gouri.Mahajan7@gmail.com ASP .Net 1 05-23-2008 12:20 PM
keydown event Claudia Fong ASP .Net 5 09-18-2007 08:52 AM
trap keypress and keydown event in asp.net mobile form jaspal singh ASP .Net Mobile 1 11-27-2006 06:17 AM



Advertisments