Hi all:
I am a newer for python and wxpython. I want to use the keyboard [UP], [DOWN] and so on key to operate the programe, how should I do?
thanks.
Hi all:
I am a newer for python and wxpython. I want to use the keyboard [UP], [DOWN] and so on key to operate the programe, how should I do?
thanks.
Hello,
Hi all:
I am a newer for python and wxpython. I want to use the keyboard [UP], [DOWN] and so on key to operate the programe, how should I do?
thanks.
In your window bind to the EVT_KEY_UP event and check the keycode for wx.WXK_UP, wx.WXK_DOWN. (Also see wx.EVT_KEY_DOWN, wx.EVT_CHAR, KeyEvents sample in the wxPython demo).
i.e)
self.Bind(wx.EVT_KEY_UP, self.OnKeyUp)
def OnKeyUp(self, evt):
code = evt.GetKeyCode()
if code == wx.WXK_UP:
do something
elif code == wx.WXK_DOWN:
do something
else:
evt.Skip()
Cody
On Dec 25, 2008, at 10:49 PM, Amao Zhao wrote:
I've had a similar problem. In order for my program to recognize the
arrow keys, I need to press the ALT button. I'm on a Dall Laptop,
running Windows XP with Python 2.6 and wxPython 2.8.9.1 (Unicode)
the code:
import wx
class TestPanel(wx.Panel):
def __init__(self,*args,**kwds):
wx.Panel.__init__(self,*args,**kwds)
sizer=wx.BoxSizer(wx.VERTICAL)
sizer.SetMinSize((300, 200))
self.Text = wx.StaticText(self, label="Key Tests\n")
sizer.Add(self.Text, 1, wx.EXPAND | wx.GROW | wx.ALL, 10)
self.SetSizerAndFit(sizer)
self.Layout()
self.SetFocus()
self.Bind(wx.EVT_CHAR, self.OnKey)
def OnKey(self, event):
print event
lbl = self.Text.GetLabel()
if event.GetKeyCode() == wx.WXK_UP:
lbl += "Up\n"
else:
lbl += "%s\n" % event.GetKeyCode()
self.Text.SetLabel(lbl)
event.Skip()
class TestFrame(wx.Frame):
def __init__(self,*args,**kwds):
wx.Frame.__init__(self,*args,**kwds)
panel = TestPanel(self,wx.ID_ANY)
sizer=wx.BoxSizer(wx.VERTICAL)
sizer.Add(panel, 1, wx.EXPAND)
self.SetSizerAndFit(sizer)
self.Layout()
class TestApp(wx.App):
def OnInit(self):
mainFrame = TestFrame(None,-1,"Test")
self.SetTopWindow(mainFrame)
mainFrame.Show()
return 1
app = TestApp()
app.MainLoop()
In fact, sometimes when I run this I can't get any character to work
without the ALT key. My keyboard is probably screwed up. I still have
a problem with KEY_UP and KEY_DOWN events firing about once a second
no matter what I do.
On Thu, Dec 25, 2008 at 8:56 PM, Cody Precord <codyprecord@gmail.> wrote:
Hello,
On Dec 25, 2008, at 10:49 PM, Amao Zhao wrote:
Hi all:
I am a newer for python and wxpython. I want to use the keyboard [UP],
[DOWN] and so on key to operate the programe, how should I do?
thanks.In your window bind to the EVT_KEY_UP event and check the keycode for
wx.WXK_UP, wx.WXK_DOWN. (Also see wx.EVT_KEY_DOWN, wx.EVT_CHAR, KeyEvents
sample in the wxPython demo).i.e)
self.Bind(wx.EVT_KEY_UP, self.OnKeyUp)
def OnKeyUp(self, evt):
code = evt.GetKeyCode()
if code == wx.WXK_UP:
do something
elif code == wx.WXK_DOWN:
do something
else:
evt.Skip()Cody
_______________________________________________
wxpython-users mailing list
wxpython-users@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users
--
Josh English
Joshua.R.English@gmail.com
Josh English wrote:
I've had a similar problem. In order for my program to recognize the
arrow keys, I need to press the ALT button. I'm on a Dall Laptop,
running Windows XP with Python 2.6 and wxPython 2.8.9.1 (Unicode)the code:
import wxclass TestPanel(wx.Panel):
def __init__(self,*args,**kwds):
wx.Panel.__init__(self,*args,**kwds)
sizer=wx.BoxSizer(wx.VERTICAL)
sizer.SetMinSize((300, 200))
self.Text = wx.StaticText(self, label="Key Tests\n")
sizer.Add(self.Text, 1, wx.EXPAND | wx.GROW | wx.ALL, 10)
self.SetSizerAndFit(sizer)
self.Layout()
self.SetFocus()self.Bind(wx.EVT_CHAR, self.OnKey)
def OnKey(self, event):
print event
lbl = self.Text.GetLabel()
if event.GetKeyCode() == wx.WXK_UP:
lbl += "Up\n"
else:
lbl += "%s\n" % event.GetKeyCode()
self.Text.SetLabel(lbl)
event.Skip()class TestFrame(wx.Frame):
def __init__(self,*args,**kwds):
wx.Frame.__init__(self,*args,**kwds)panel = TestPanel(self,wx.ID_ANY)
sizer=wx.BoxSizer(wx.VERTICAL)
sizer.Add(panel, 1, wx.EXPAND)
self.SetSizerAndFit(sizer)
self.Layout()class TestApp(wx.App):
def OnInit(self):
mainFrame = TestFrame(None,-1,"Test")
self.SetTopWindow(mainFrame)
mainFrame.Show()
return 1app = TestApp()
app.MainLoop()In fact, sometimes when I run this I can't get any character to work
without the ALT key. My keyboard is probably screwed up. I still have
a problem with KEY_UP and KEY_DOWN events firing about once a second
no matter what I do.
While it does sound like you have a goofy keyboard (maybe someone spilled something on it?), the primary issue is that wx just doesn't like catching key events this way. Cody was pointing this out to a newb yesterday that wx.Frame doesn't accept focus (and wx.Panel seems flakey about it to me) and that the OP should use an AcceleratorTable instead. I think that's what's needed here too. It should be noted that there is now a bug in the 2.8.9.x series that seems to require a menubar for the AcceleratorTable to work. It wasn't there in the previous series. Anyway, I've modified your code to show you one way to do it:
<code>
import wx
upId = wx.NewId()
class TestPanel(wx.Panel):
def __init__(self,*args,**kwds):
wx.Panel.__init__(self,*args,**kwds)
sizer=wx.BoxSizer(wx.VERTICAL)
sizer.SetMinSize((300, 200))
self.Text = wx.StaticText(self, label="Key Tests\n")
sizer.Add(self.Text, 1, wx.EXPAND | wx.GROW | wx.ALL, 10)
self.SetSizerAndFit(sizer)
self.Layout()
self.SetFocus()
class TestFrame(wx.Frame):
def __init__(self,*args,**kwds):
wx.Frame.__init__(self,*args,**kwds)
self.panel = TestPanel(self,wx.ID_ANY)
sizer=wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.panel, 1, wx.EXPAND)
self.SetSizerAndFit(sizer)
menuBar = wx.MenuBar()
self.SetMenuBar(menuBar)
self.Bind(wx.EVT_MENU, self.OnKey, id=upId)
accel_tbl = wx.AcceleratorTable([(wx.ACCEL_NORMAL, wx.WXK_UP, upId) ])
self.SetAcceleratorTable(accel_tbl)
self.Layout()
def OnKey(self, event):
print event lbl = self.panel.Text.GetLabel()
if event.GetId() == upId:
lbl += "Up\n"
else:
lbl += "%s\n" % event.GetKeyCode()
self.panel.Text.SetLabel(lbl)
event.Skip()
class TestApp(wx.App):
def OnInit(self):
mainFrame = TestFrame(None,-1,"Test")
self.SetTopWindow(mainFrame)
mainFrame.Show()
return 1
app = TestApp()
app.MainLoop()
</code>
It's not quite as nice as it would be to just bind to EVT_CHAR, but I think with a little work, this could be refactored to do it almost as easily.
On Thu, Dec 25, 2008 at 8:56 PM, Cody Precord <codyprecord@gmail.> wrote:
Hello,
On Dec 25, 2008, at 10:49 PM, Amao Zhao wrote:
Hi all:
I am a newer for python and wxpython. I want to use the keyboard [UP],
[DOWN] and so on key to operate the programe, how should I do?
thanks.
In your window bind to the EVT_KEY_UP event and check the keycode for
wx.WXK_UP, wx.WXK_DOWN. (Also see wx.EVT_KEY_DOWN, wx.EVT_CHAR, KeyEvents
sample in the wxPython demo).i.e)
self.Bind(wx.EVT_KEY_UP, self.OnKeyUp)
def OnKeyUp(self, evt):
code = evt.GetKeyCode()
if code == wx.WXK_UP:
do something
elif code == wx.WXK_DOWN:
do something
else:
evt.Skip()Cody
_______________________________________________
wxpython-users mailing list
wxpython-users@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users
-------------------
Mike Driscoll
Blog: http://blog.pythonlibrary.org
Python Extension Building Network: http://www.pythonlibrary.org
Josh English wrote:
I've had a similar problem. In order for my program to recognize the
arrow keys, I need to press the ALT button. I'm on a Dall Laptop,
running Windows XP with Python 2.6 and wxPython 2.8.9.1 (Unicode)
Try adding the wx.WANTS_CHARS flag to the style value passed to the wx.Panel constructor.
--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!