[Newbie] KeyEvent

Hello,

I'm new to wxPython and find myself now confrontated with object-orientated programming. Nevertheless, i try to get some simple things working :slight_smile:

My goal is simple to evaluate pressed keys. I have written a small program, which uses wx.KeyEvent. But always when i press a key, nothing really changes. But here is my code:

# +++++++++++ #

import wx

app = wx.PySimpleApp()

while 1:
  Key = wx.KeyEvent()

  KeyCode = Key.GetRawKeyCode()
  Event = Key.GetEventType()
  EventObject = Key.GetEventObject()
  UnicodeKey = Key.GetUnicodeKey()
  KeyFlags = Key.GetRawKeyFlags()
  UniChar = Key.GetUniChar()
  
  nKeyCode = Key.KeyCode()
  
  print (KeyCode, Event, EventObject, UnicodeKey, KeyFlags, UniChar, nKeyCode)

app.MainLoop()

# +++++++++++ #

Franz Hoehne

Franz wrote:

Hello,

I'm new to wxPython and find myself now confrontated with object-orientated programming. Nevertheless, i try to get some simple things working :slight_smile:

My goal is simple to evaluate pressed keys. I have written a small program, which uses wx.KeyEvent. But always when i press a key, nothing really changes.

You can create a KeyEvent, but in that case, you need to setup the keycodes as well. Your program will process an infinite loop and it will never reach app.MainLoop().

Alternatively, wxWidgets can automatically create a new wx.KeyEvent when you press the button. To do this, create a frame. Bind the frame to the event, and call MainLoop(). wxWidgets will create the new event for you at the time when you press the key, and (since you bound it to an event handler) your event handler will be called. Try this example:

import wx

app = wx.PySimpleApp()

class MyFrame(wx.Frame):
      def __init__(self,*args,**kwargs):
          wx.Frame.__init__(self,*args,**kwargs)
          self.Bind(
             wx.EVT_KEY_DOWN,
             self.OnKey,
             self
          )
      def OnKey(self,event):
        KeyCode = event.GetRawKeyCode()
        Event = event.GetEventType()
        EventObject = event.GetEventObject()
        UnicodeKey = event.GetUnicodeKey()
        KeyFlags = event.GetRawKeyFlags()
        UniChar = event.GetUniChar()
        nKeyCode = event.KeyCode()
        print (KeyCode, Event, EventObject, UnicodeKey, KeyFlags, UniChar, nKeyCode)

frm = MyFrame(None,-1)
frm.Show(True)
app.MainLoop()

You can notice that:

1. MainLoop contains the infinite loop, but it is very effective because it uses system interrupts to listen to events.
2. The event handler is only called when there is an event. CPU is at 0% if you don't touch anything.
3. You can bind to EVT_KEY_DOWN EVT_KEY_UP and EVT_CHAR

Please find a wxPython tutorial and go over it.

   Les

Franz,
try using the Getting Started guide
http://wiki.wxpython.org/index.cgi/Getting_20Started
or
http://wiki.wxpython.org/index.cgi/GettingStarted

take the small steps you'll find there.... read as much as you can about the philosophy of GUI programming...
Creating GUI applications is not only about leaning a new API, you'll have to understand what a GUI is, how it suppose to react to user input, how user input is handled.

the MainLoop is used to dispatch the events, everything in a GUI runs in there. If you want to catch key presses you'll have to Bind an event handler for that kind of input to a component that can receive keyboard input.

follow the simple examples from the Getting Started guide... it doesn't take all that much...

Peter.

···

On Mon, 03 Oct 2005 18:37:03 +0300, Franz <franzlinux@gmx.de> wrote:

Hello,

I'm new to wxPython and find myself now confrontated with object-orientated programming. Nevertheless, i try to get some simple things working :slight_smile: