Catching ESC key in FRAME?

hi all.

I have a frame with no menu to give you the choice to destroy the frame pressing ESC.

Is it possible do that?

what is the event associated with the frame (wx.EVT_…?):

class LoadCal(wx.Frame):

def init(self,parent,posxy):

wx.Frame.init(self,parent,-1,‘cal’,size=(190,140))

self.SetFont(wx.Font(8,wx.SWISS,wx.NORMAL,wx.NORMAL,False))

self.Bind(wx.calendar.EVT_CALENDAR, self.OnCalSelected, id=cal.GetId())

self.Bind(wx.EVT_?.., self.OnEventX)

def OnEventX(self, evt):

if evt.GetKeyCode() == wx.WXK_ESCAPE: #

self.Destroy()

···

Fabio Spadaro

www.fabiospadaro.com

Hi,

···

On Wed, Dec 9, 2009 at 9:42 AM, Fabio Spadaro <fabiolinospad@gmail.com> wrote:

hi all.
I have a frame with no menu to give you the choice to destroy the frame
pressing ESC.
Is it possible do that?
what is the event associated with the frame (wx.EVT_...?):

class LoadCal(wx.Frame):
def __init__(self,parent,posxy):
wx.Frame.__init__(self,parent,-1,'cal',size=(190,140))
self.SetFont(wx.Font(8,wx.SWISS,wx.NORMAL,wx.NORMAL,False))
self.Bind(wx.calendar.EVT_CALENDAR, self.OnCalSelected,
id=cal.GetId())
self.Bind(wx.EVT_?..., self.OnEventX)

def OnEventX(self, evt):
if evt.GetKeyCode() == wx.WXK_ESCAPE: #
self.Destroy()

see: wx.AcceleratorTable

Cody

But I do not use menu …

···

2009/12/9 Cody Precord codyprecord@gmail.com

Hi,

On Wed, Dec 9, 2009 at 9:42 AM, Fabio Spadaro fabiolinospad@gmail.com wrote:

hi all.
I have a frame with no menu to give you the choice to destroy the frame

pressing ESC.
Is it possible do that?
what is the event associated with the frame (wx.EVT_…?):

class LoadCal(wx.Frame):
def init(self,parent,posxy):
wx.Frame.init(self,parent,-1,‘cal’,size=(190,140))

   self.SetFont(wx.Font(8,wx.SWISS,wx.NORMAL,wx.NORMAL,False))
   self.Bind(wx.calendar.EVT_CALENDAR, self.OnCalSelected,

id=cal.GetId())
self.Bind(wx.EVT_?.., self.OnEventX)

def OnEventX(self, evt):
if evt.GetKeyCode() == wx.WXK_ESCAPE: #
self.Destroy()

see: wx.AcceleratorTable

Cody

To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en


Fabio Spadaro
www.fabiospadaro.com

Hi,

But I do not use menu ...

You don't need a menu to use an AcceleratorTable.

example:

import wx

class MyFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, title="Test",
style=wx.DEFAULT_FRAME_STYLE)

        self._panel = wx.Panel(self)
        self._tbl = wx.AcceleratorTable([(wx.ACCEL_CTRL, ord('q'),
wx.ID_CLOSE)])
        self.SetAcceleratorTable(self._tbl)

        self.Bind(wx.EVT_MENU, self.OnAccel, id=wx.ID_CLOSE)

    def OnAccel(self, evt):
        print "HELLO"

if __name__ == "__main__":
    app = wx.App(False)
    frame = MyFrame()
    frame.Show()
    app.MainLoop()

Regards,

Cody

(p.s please don't top post)

···

On Wed, Dec 9, 2009 at 10:38 AM, Fabio Spadaro <fabiolinospad@gmail.com> wrote:

I'm not sure if it was fixed before or after the current release, but there was a bug on Windows where the accelerator table would not work if there wasn't a menubar on the frame.

···

On 12/9/09 9:02 AM, Cody Precord wrote:

Hi,

On Wed, Dec 9, 2009 at 10:38 AM, Fabio Spadaro<fabiolinospad@gmail.com> wrote:

But I do not use menu ...

You don't need a menu to use an AcceleratorTable.

--
Robin Dunn
Software Craftsman

Hi,

Hi,

But I do not use menu ...

You don't need a menu to use an AcceleratorTable.

I'm not sure if it was fixed before or after the current release, but
there was a bug on Windows where the accelerator table would not work if
there wasn't a menubar on the frame.

Looks like it was fixed quite sometime ago
(wxTrac has been migrated to GitHub Issues - wxWidgets) works fine on 2.8.10 for me.

Escape does not seem to work on Windows though using this approach.
Using EVT_CHAR_HOOK does work though.

self.Bind(wx.EVT_CHAR_HOOK, self.OnHook)

def OnHook(self, evt):
    if evt.GetKeyCode() == wx.WXK_ESCAPE:
        print "ESCAPED"
    else:
        evt.Skip()

Cody

···

On Wed, Dec 9, 2009 at 2:28 PM, Robin Dunn <robin@alldunn.com> wrote:

On 12/9/09 9:02 AM, Cody Precord wrote:

On Wed, Dec 9, 2009 at 10:38 AM, Fabio Spadaro<fabiolinospad@gmail.com> wrote:

your example does wotk but I would like to work with wx.WXK_ESCAPE instead ctrl-Q.
is it possible?

···

2009/12/9 Cody Precord codyprecord@gmail.com

Hi,

On Wed, Dec 9, 2009 at 10:38 AM, Fabio Spadaro fabiolinospad@gmail.com wrote:

But I do not use menu …

You don’t need a menu to use an AcceleratorTable.

example:

import wx

class MyFrame(wx.Frame):

def __init__(self):

    wx.Frame.__init__(self, None, title="Test",

style=wx.DEFAULT_FRAME_STYLE)

    self._panel = wx.Panel(self)

    self._tbl = wx.AcceleratorTable([(wx.ACCEL_CTRL, ord('q'),

wx.ID_CLOSE)])

    self.SetAcceleratorTable(self._tbl)



    self.Bind(wx.EVT_MENU, self.OnAccel, id=wx.ID_CLOSE)



def OnAccel(self, evt):

    print "HELLO"

if name == “main”:

app = wx.App(False)

frame = MyFrame()

frame.Show()

app.MainLoop()

Regards,

Cody

(p.s please don’t top post)

To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com

or visit http://groups.google.com/group/wxPython-users?hl=en


Fabio Spadaro

www.fabiospadaro.com

Hi,

···

On Wed, Dec 9, 2009 at 2:39 PM, Fabio Spadaro fabiolinospad@gmail.com wrote:

your example does wotk but I would like to work with wx.WXK_ESCAPE instead ctrl-Q.

is it possible?

I’m not even sure what you’re asking here. Do you want to use Escape as you originally asked or did you change your mind to wanting to use CTRL+Q? Cody told you how to do it with ESC. If you want to use CTRL+ some letter, try reading the docs:

http://www.wxpython.org/docs/api/wx.KeyEvent-class.html

That link lists all the key code stuff you’ll ever need. Also, here’s a link to a tutorial of mine on using Accelerator Tables:

http://www.blog.pythonlibrary.org/2008/07/02/wxpython-working-with-menus-toolbars-and-accelerators/


Mike Driscoll

Blog: http://blog.pythonlibrary.org

Hi.

Using EVT_CHAR_HOOK it does work.

Thanks

···

2009/12/9 Cody Precord codyprecord@gmail.com

Hi,

On Wed, Dec 9, 2009 at 2:28 PM, Robin Dunn robin@alldunn.com wrote:

On 12/9/09 9:02 AM, Cody Precord wrote:

Hi,

On Wed, Dec 9, 2009 at 10:38 AM, Fabio Spadarofabiolinospad@gmail.com wrote:

But I do not use menu …

You don’t need a menu to use an AcceleratorTable.

I’m not sure if it was fixed before or after the current release, but
there was a bug on Windows where the accelerator table would not work if

there wasn’t a menubar on the frame.

Looks like it was fixed quite sometime ago
(http://trac.wxwidgets.org/ticket/9784) works fine on 2.8.10 for me.

Escape does not seem to work on Windows though using this approach.
Using EVT_CHAR_HOOK does work though.

self.Bind(wx.EVT_CHAR_HOOK, self.OnHook)

def OnHook(self, evt):
if evt.GetKeyCode() == wx.WXK_ESCAPE:

print “ESCAPED”
else:
evt.Skip()

Cody


To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en


Fabio Spadaro
www.fabiospadaro.com