[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-devel
Subject:    Re: Strange behaviour of keyPressEvent and KActions with shortcut
From:       Jeroen Wijnhout <Jeroen.Wijnhout () kdemail ! net>
Date:       2003-07-21 7:36:13
[Download RAW message or body]

On Monday 21 July 2003 00:00, Albert 'TSDgeos' Astals Cid wrote:
> If it define a KAction with Left_arrow as shortcut, the widget's
> keyPressEvent function never receives an event for the press of Left_arrow.
> This is a strange behaviour as keyPressEvent should receive all the key
> events that happen in that widget (i think that is reasonable).
>
> keyReleaseEvent does receive the release event.

I think the action receives the keyPress/Release Events, processes it and 
sends a AccelOverride event. This way you know the key has been processed, if 
you would receive a keyPressEvent there would be now way to tell if the 
kaction has processed the key or not.

So, install an eventfilter and check for AccelOverride events and process 
these.

best,
Jeroen

> Some code illustrating that.
>
>
> #include <kapplication.h>
> #include <kcmdlineargs.h>
> #include <kaboutdata.h>
> #include <kaction.h>
> #include <kmainwindow.h>
>
> class MyWidget : public KMainWindow
> {
> 	public:
> 		MyWidget();
> 	protected:
> 		virtual void keyPressEvent( QKeyEvent *event );
> 		virtual void keyReleaseEvent( QKeyEvent *event );
>
> 	private:
> 		KAction *mAction;
> };
>
> MyWidget::MyWidget() : KMainWindow(0, 0, WType_Modal)
> {
> 	mAction = new KAction(i18n("Move Left"), Qt::Key_Left, 0, 0,
> actionCollection(), "move_left");
> 	resize(500, 500);
> 	show();
> }
>
> void MyWidget::keyPressEvent(QKeyEvent* event)
> {
> 	setCaption("Key Pressed");
> 	KKey key(event);
> 	if (mAction->shortcut().contains(key))
> 	{
> 		setCaption("Left Key Pressed");
> 	}
> 	else event->ignore();
> }
>
> void MyWidget::keyReleaseEvent(QKeyEvent* event)
> {
> 	setCaption("Key Released");
> 	KKey key(event);
> 	if (mAction->shortcut().contains(key))
> 	{
> 		setCaption("Left Key Released");
> 	}
> 	else event->ignore();
> }
>
> int main(int argc, char *argv[])
> {
> 	KAboutData *data = new KAboutData("a", "", "", "", 1, "",  0, 0, "");
> 	KCmdLineArgs::init(argc, argv, data);
> 	KApplication a(argc, argv);
> 	MyWidget *x = new MyWidget();
> 	a.setMainWidget(x);
> 	return a.exec();
> }
>
> >> Visit http://mail.kde.org/mailman/listinfo/kde-devel#unsub to
> >> unsubscribe <<

-- 
Kile - a LaTeX editor for KDE
http://kile.sourceforge.net
 
>> Visit http://mail.kde.org/mailman/listinfo/kde-devel#unsub to unsubscribe <<
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic