2 Copyright (C) 2002-2003 Tim Jansen <tim@tjansen.de>
3 Copyright (C) 2004 Nadeem Hasan <nhasan@kde.org>
5 /***************************************************************************
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
12 ***************************************************************************/
15 // based on key capture code from kdelibs/kdeui/kshortcutdialog.cpp
18 #include "keycapturedialog.h"
19 #include "keycapturewidget.h"
22 #include <QVBoxLayout>
29 #include <X11/Xlib.h> // For x11Event()
30 #include <X11/keysymdef.h> // For XK_...
33 const int XFocusOut
= FocusOut
;
34 const int XFocusIn
= FocusIn
;
35 const int XKeyPress
= KeyPress
;
36 const int XKeyRelease
= KeyRelease
;
44 KeyCaptureDialog::KeyCaptureDialog(QWidget
*parent
, const char *name
)
45 : KDialog( parent
), m_grabbed(false) {
46 setObjectName( name
);
48 setCaption( i18n( "Enter Key Combination" ) );
50 setDefaultButton( Cancel
);
51 showButtonSeparator( true );
53 QFrame
*main
= new QFrame();
54 setMainWidget( main
);
56 QVBoxLayout
*layout
= new QVBoxLayout( main
);
57 layout
->setSpacing( KDialog::spacingHint() );
58 layout
->setMargin( 0 );
59 m_captureWidget
= new KeyCaptureWidget( main
, "m_captureWidget" );
60 layout
->addWidget( m_captureWidget
);
64 KeyCaptureDialog::~KeyCaptureDialog() {
69 void KeyCaptureDialog::execute() {
70 m_captureWidget
->keyLabel
->setText("");
76 bool KeyCaptureDialog::x11Event(XEvent
*pEvent
)
78 switch( pEvent
->type
) {
81 x11EventKeyPress( pEvent
);
94 return QWidget::x11Event( pEvent
);
97 void KeyCaptureDialog::x11EventKeyPress( XEvent
*pEvent
)
103 // taken from kshortcutdialog.h
104 KKeyNative
keyNative( pEvent
);
105 uint keyModX
= keyNative
.mod(), keySymX
= keyNative
.sym();
106 if ((keySymX
== XK_Escape
) && !keyModX
) {
112 // Don't allow setting a modifier key as an accelerator.
113 // Also, don't release the focus yet. We'll wait until
114 // we get a 'normal' key.
115 case XK_Shift_L
: case XK_Shift_R
: keyModX
= KKeyNative::modXShift(); break;
116 case XK_Control_L
: case XK_Control_R
: keyModX
= KKeyNative::modXCtrl(); break;
117 case XK_Alt_L
: case XK_Alt_R
: keyModX
= KKeyNative::modXAlt(); break;
118 // FIXME: check whether the Meta or Super key are for the Win modifier
119 case XK_Meta_L
: case XK_Meta_R
:
120 case XK_Super_L
: case XK_Super_R
: keyModX
= KKeyNative::modXWin(); break;
121 case XK_Hyper_L
: case XK_Hyper_R
:
127 if( pEvent
->type
== XKeyPress
&& keyNative
.sym() ) {
128 emit
keyPressed(pEvent
);
134 // If we are editing the first key in the sequence,
135 // display modifier keys which are held down
136 if( pEvent
->type
== XKeyPress
)
137 keyModX
|= pEvent
->xkey
.state
;
139 keyModX
= pEvent
->xkey
.state
& ~keyModX
;
142 if( keyModX
& KKeyNative::modXWin() ) keyModStr
+= KKey::modFlagLabel(KKey::WIN
) + '+';
143 if( keyModX
& KKeyNative::modXAlt() ) keyModStr
+= KKey::modFlagLabel(KKey::ALT
) + '+';
144 if( keyModX
& KKeyNative::modXCtrl() ) keyModStr
+= KKey::modFlagLabel(KKey::CTRL
) + '+';
145 if( keyModX
& KKeyNative::modXShift() ) keyModStr
+= KKey::modFlagLabel(KKey::SHIFT
) + '+';
147 // Display currently selected modifiers, or redisplay old key.
148 m_captureWidget
->keyLabel
->setText( keyModStr
);
152 #include "keycapturedialog.moc"