2 // keygrabber.cpp - Widget to grab a key and dismiss itself
5 // (C) 2011 Underground Software
7 // JLH = James Hammons <jlhamm@acm.org>
10 // --- ---------- ------------------------------------------------------------
11 // JLH 07/18/2011 Created this file
14 #include "keygrabber.h"
19 // These need to be preserved between calls to this class, otherwise bad stuff
20 // (like controllers not working correctly) can happen.
21 /*static*/ bool KeyGrabber::buttonDown = false;
22 /*static*/ int KeyGrabber::button = -1;
25 KeyGrabber::KeyGrabber(QWidget * parent/*= 0*/): QDialog(parent),
26 label(new QLabel), timer(new QTimer)//, buttonDown(false)
28 // label = new QLabel(this);
29 QVBoxLayout * mainLayout = new QVBoxLayout;
30 mainLayout->addWidget(label);
31 setLayout(mainLayout);
32 setWindowTitle(tr("Grab"));
33 connect(timer, SIGNAL(timeout()), this, SLOT(CheckGamepad()));
34 timer->setInterval(100);
37 // Will this make Mac OSX work???
38 setFocusPolicy(Qt::StrongFocus);
42 KeyGrabber::~KeyGrabber()
48 void KeyGrabber::SetKeyText(int keyNum)
50 char jagButtonName[21][10] = { "Up", "Down", "Left", "Right",
51 "*", "7", "4", "1", "0", "8", "5", "2", "#", "9", "6", "3",
52 "A", "B", "C", "Option", "Pause" };
54 QString text = QString(tr("Press key for \"%1\"<br>(ESC to cancel)"))
55 .arg(QString(jagButtonName[keyNum]));
60 void KeyGrabber::keyPressEvent(QKeyEvent * e)
64 // Since this is problematic, we don't allow this key...
65 if (key != Qt::Key_Alt)
70 void KeyGrabber::CheckGamepad(void)
72 // How do we determine which joystick it is, if more than one? As it turns
73 // out, we don't really have to care. It's up to the user to play nice with
74 // the interface because while we can enforce a 'first user to press a
75 // button wins' type of thing, it doesn't really buy you anything that you
76 // couldn't get by having the users involved behave like nice people. :-P
81 button = Gamepad::CheckButtonPressed();
86 // Do it so that it sets the button on button down, not release :-P
93 if (Gamepad::CheckButtonPressed() == button)