| View previous topic :: View next topic |
| Author |
Message |
Evil_Intentions Expert Cheater
Reputation: 65
Joined: 07 Jan 2010 Posts: 214
|
Posted: Thu Nov 18, 2010 9:37 pm Post subject: Nested(?) Keypress Checks VB |
|
|
Note: If this is possible in C++ i could also do it that way.
I would like to know if it is possible to check if a key is pressed while pressing another key. I want to make a small ocarina simulation program. Where 5 buttons at the most will be pressed at once.
Pseudocode:
check if the space key is pressed
if it is, check if the B key is pressed
if it is, check if the N key is pressed
......
if it is not, check if the G key is pressed
Get what im saying. The space key will act like air, which will begin the virtual instrument. Then depending on the combination of G,B,N,and J keys, different tones will be played.
In other words, all 5 keys held down will play a C tone/note. Having all notes except J pressed results in the note "D" being played. |
|
| Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Fri Nov 19, 2010 3:45 am Post subject: |
|
|
When you receive a key down event, store that it's being pressed. While receiving an another event, check the keys that are already down and do whatever you want. When you get a key up event, just remove the button from the list.
C++ example:
keywidget.h:
| Code: | #ifndef KEYWIDGET_H
#define KEYWIDGET_H
#include <QWidget>
#include <QLabel>
#include <QList>
class KeyWidget : public QWidget
{
Q_OBJECT;
private:
QLabel *m_caption;
QLabel *m_keys;
QString m_pressedKeys;
protected:
virtual void keyPressEvent(QKeyEvent *event);
virtual void keyReleaseEvent(QKeyEvent *event);
public:
KeyWidget(QWidget *parent = 0, Qt::WindowFlags f = 0);
~KeyWidget();
};
#endif // KEYWIDGET_H |
keywidget.cpp:
| Code: | #include "keywidget.h"
#include <QKeyEvent>
#include <QVBoxLayout>
KeyWidget::KeyWidget(QWidget *parent /* = 0 */, Qt::WindowFlags f /* = 0 */)
{
this->m_caption = new QLabel("Key IDs:", this);
this->m_caption->setAlignment(Qt::AlignCenter);
this->m_keys = new QLabel(this);
this->m_keys->setAlignment(Qt::AlignCenter);
QVBoxLayout *layout = new QVBoxLayout();
layout->addWidget(this->m_caption);
layout->addWidget(this->m_keys);
this->setLayout(layout);
this->setFocusPolicy(Qt::StrongFocus);
this->setFocus();
}
KeyWidget::~KeyWidget()
{
}
void KeyWidget::keyPressEvent(QKeyEvent *event)
{
QString s = event->text();
if( s == " " )
s = " SPACE ";
else
s = " " + s + " ";
this->m_pressedKeys += s;
this->m_keys->setText(this->m_pressedKeys);
QWidget::keyPressEvent(event);
}
void KeyWidget::keyReleaseEvent(QKeyEvent *event)
{
QString s = event->text();
if( s == " " )
s = " SPACE ";
else
s = " " + s + " ";
this->m_pressedKeys.remove(s);
this->m_keys->setText(this->m_pressedKeys);
QWidget::keyReleaseEvent(event);
} |
|
|
| Back to top |
|
 |
Gekko92 How do I cheat?
Reputation: 0
Joined: 19 Nov 2010 Posts: 1
|
Posted: Fri Nov 19, 2010 4:49 pm Post subject: Re: Nested(?) Keypress Checks VB |
|
|
| Z++ wrote: | Note: If this is possible in C++ i could also do it that way.
I would like to know if it is possible to check if a key is pressed while pressing another key. I want to make a small ocarina simulation program. Where 5 buttons at the most will be pressed at once.
Pseudocode:
check if the space key is pressed
if it is, check if the B key is pressed
if it is, check if the N key is pressed
......
if it is not, check if the G key is pressed
Get what im saying. The space key will act like air, which will begin the virtual instrument. Then depending on the combination of G,B,N,and J keys, different tones will be played.
In other words, all 5 keys held down will play a C tone/note. Having all notes except J pressed results in the note "D" being played. |
So you want to know how to check for keystrokes in VB?
use this:
Declerations
| Code: |
Public Declare Function GetAsyncKeyState Lib "user32.dll" (ByVal vKey As Int32) As Boolean |
Check if a keystroke is pressed
| Code: | If GetAsyncKeyState(keys.f12) = true then
MsgBox("You Pressed F12!")
End If
|
Its easy to check for multiple keystrokes at the same time.
| Code: | If GetAsyncKeyState(keys.f12) = true then
If GetAsyncKeyState(keys.f11) = true then
MsgBox("You just pressed F11 and F12!")
End If
End If |
|
|
| Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Fri Nov 19, 2010 5:59 pm Post subject: |
|
|
| you should generally be using the window messages for input unless you have a good reason not to. |
|
| Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Sat Nov 20, 2010 5:13 am Post subject: |
|
|
| slovach wrote: | | you should generally be using the window messages for input unless you have a good reason not to. | I second this, event driven programming is much more efficient than polling based. So, don't do GetAsyncKeyState, but instead catch the key press and release events. |
|
| Back to top |
|
 |
|