Window->KeyDown event (holding down multiple keys)

Hey guys,

I've been attempting to set up an event that checks for multiple keys pressed down at the same time within a windows 8 store app with no luck yet.

Below is one of my attempts that hasn't borne fruit.

Window->KeyDown += ref new TypedEventHandler<CoreWindow^, KeyEventArgs^>(this, &App::KeyDown);

//When key is pressed down
void KeyDown(CoreWindow^ Window, KeyEventArgs^ Args)
{
//if both up and left are pressed
if ((Args->VirtualKey == VirtualKey::Up) && (Args->VirtualKey == VirtualKey::Left)) {
MessageDialog Dialog("Up and left pressed!", "It's working!");
Dialog.ShowAsync();
}

Currently if I hold two buttons down, it only registers the one I pushed second. Has anyone implemented an event which involved multiple keys being pressed down for Windows 8 store apps? Any help would be greatly appreciated as always =) The methods I have seen lying around for multiple key presses are for older OS.
I haven't tried making windows 8 store apps (due to a lack of owning windows 8), but the concept should be fairly similar to older versions.

Have you tried having an array of bools to store if keys are being pressed, and then testing for the keypress and keyrelease objects to turn the values off and on?

e.g. (based off your code):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
bool keyDownArray[256] = {false};  // keys array
// OR, if the keys aren't stored as numerical values anymore...
bool leftKey = false;
bool upKey = false;
// ...


Window->KeyDown += ref new TypedEventHandler<CoreWindow^, KeyEventArgs^>
    (this, &App::KeyDown);
Window->KeyUp += ref new TypedEventHandler<CoreWindow^, KeyEventArgs^>
    (this, &App::KeyUp);

void KeyDown(CoreWindow^ Window, KeyEventArgs^ Args) {
    // either
    keyDownArray[Args->VirtualKey] = true;
    // or
    if (Args->VirtualKey == VirtualKey::Up)
        upKey = true;
    if (Args->VirtualKey == VirtualKey::Left)
        leftKey = true;
}

void KeyUp(CoreWindow^ Window, KeyEventArgs^ Args) {
    // either
    keyDownArray[Args->VirtualKey] = false;
    // or
    if (Args->VirtualKey == VirtualKey::Up)
        upKey = false;
    if (Args->VirtualKey == VirtualKey::Left)
        leftKey = false;
}

// ....

if (keyDownArray[VirtualKey::Up] && keyDownArray[VirtualKey::Left]) {
    MessageDialog Dialog("Up and left pressed!", "It's working!");
    Dialog.ShowAsync();
}

// OR

if (keyLeft && keyUp) {
    MessageDialog Dialog("Up and left pressed!", "It's working!");
    Dialog.ShowAsync()
}


As I said, I haven't done any windows 8 store apps before, so this may be completely wrong, but hopefully it gives you an idea for a possible solution that you can implement.

EDIT:
Forgot to finish example...
Last edited on
Brilliant. Hadn't considered using boolean values with keyup and down events. Seems an obvious option now.

Many thanks =)
Brilliant. Hadn't considered using boolean values with keyup and down events. Seems an obvious option now.

Many thanks =)
Topic archived. No new replies allowed.