Re: Key presses and releases



I don't know why your code doesn't work. Look at this, I've written
and tested this code:

bool hasToggled;

bool keyPressHandler(GdkEventKey* event) {
  if(event->type == GDK_KEY_PRESS && event->keyval == GDK_space &&
!hasToggled) {
    hasToggled = true;
    cout << "Toggled (active)" << endl;
  }
  else if(event->type == GDK_KEY_RELEASE && event->keyval == GDK_space
&& hasToggled) {
    hasToggled = false;
    cout << "Toggled (inactive)" << endl;
  }
  return false;
}

gint main(int argc, char *argv[]) {
  Gtk::Main program(argc, argv);
  Gtk::Window wnd;
  hasToggled = false;
  wnd.signal_key_press_event().connect(sigc::ptr_fun(&keyPressHandler), false);
  wnd.signal_key_release_event().connect(sigc::ptr_fun(&keyPressHandler),
false);
  program.run(wnd);
  return 0;
}

Try to compile and run it. Works perfectly, I think it's exactly what
you need. Write if you have any problems.

2009/12/23, Lyle Underwood <lyleunderwood gmail com>:
> That's what I'm doing now. The problem is that the signals don't seem to
> fire in this way. I expected the same behavior you did, I thought it
> would work like this:
>
> pressed event <-- // spacebar held down
> released event <-- // spacebar let go after a couple seconds
>
> pressed event <-- // spacebar held down
> released event <-- // spacebar let go after a couple seconds
>
> pressed event <-- // spacebar held down
> released event <-- // spacebar let go after a couple seconds
>
> But it actually does this:
>
> pressed event // <-- spacebar held down
> released event
> pressed event
> released event
> pressed event
> released event
> pressed event
> released event
> pressed event
> released event // <-- spacebar let go after a couple seconds
>
> pressed event // <-- spacebar held down
> released event
> pressed event
> released event
> pressed event
> released event
> pressed event
> released event
> pressed event
> released event // <-- spacebar let go after a couple seconds
>
> This results in the keyAlreadyPressed bool flipping between true and
> false over and over in quick succession while the space bar is
> depressed.
>
> On Wed, 2009-12-23 at 12:42 +0600, Галымжан Кожаев wrote:
>> Hi,
>>
>> Maybe you can do like this:
>>
>> void onKeyPressed(...) {
>>   if(!keyAlreadyPressed) {
>>      // space toggled (now active)
>>      keyAlreadyPressed = true;
>>      //... additional handling
>>   }
>> }
>>
>> void onKeyReleased(...) {
>>    // space toggled (now inactive)
>>    keyAlreadyPressed = false;
>> }
>>
>> keyAlreadyPressed is a bool private member or global variable.
>>
>> 2009/12/23, Lyle Underwood <lyleunderwood gmail com>:
>> > I'm so close to finishing up this application now, I'm just putting some
>> > polish on it.
>> >
>> > I have a drawing area in a viewport for scrolling. I want to add the
>> > ability to pan with the mouse, but since the user is usually drawing
>> > onto the drawing area I wanted them to use the spacebar to pan. So, you
>> > hold down spacebar and drag around to pan, just like in photoshop, gimp,
>> > etc..
>> >
>> > I'm pretty sure I'm not going to have any problem with the actual
>> > panning, but I can't figure out how to use the spacebar as a toggled
>> > state. It seems the events are triggered by some kind of key repeat,
>> > because when I hold down space I get a huge series of press events and
>> > release events. So is there some way that I can tell if the spacebar is
>> > held down constantly? I was looking through the GdkEventKey struct and
>> > couldn't find a solution.
>> >
>> > Also, I wanted to ask, what's the easiest way to get the selected row in
>> > a TreeView to redraw?
>> >
>> > Thanks.
>> >
>> > _______________________________________________
>> > gtkmm-list mailing list
>> > gtkmm-list gnome org
>> > http://mail.gnome.org/mailman/listinfo/gtkmm-list
>> >
>
>
>


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]