-1

I need to detect arrow keystrokes in the KeyDown event. I do understand that they need to be set as input keys but I'm not clear on how to accomplish it in C++ . Found good answers here, here and here. But that is C# and I need C++. Tried implementing like described here have this code in PreviewKeyDown but no luck.

switch (e->KeyCode)
{
    case Keys::Down:
    case Keys::Up:
    case Keys::Right:
    case Keys::Left:
    case Keys::Space:
    e->IsInputKey = true;
       break;
}

and my KeyDown have :

if (e->KeyCode == Keys::Left)
{
    ///
}

Which is not working on pressing left.

What am I missing?

Community
  • 1
  • 1
Divins Mathew
  • 2,908
  • 4
  • 22
  • 34
  • What do you want us to say? You already linked the relevant links that answer your question. Consider posting your code, and describing the problem completely. :) – Rakete1111 Jun 01 '16 at 18:34
  • Im sorry if Im not clear enough. I want to implement it in c++. I dont know how to translate the code in the links for cpp..and my problem is, to be more clear, the KeyDown event is detecting all the keys except the arrow keys, return, tab and space.. so i think what i need is the cpp code to properly override the IsInputKey property – Divins Mathew Jun 01 '16 at 19:09
  • You don't override it, you should call it in previewkeydown, as explained in your last link ;) – Rakete1111 Jun 01 '16 at 19:11
  • well , i said i tried taht...this is the code in my previewkeydown. doesnt work.. previewkeydown – Divins Mathew Jun 01 '16 at 19:16
  • switch (e->KeyCode) { case Keys::Down: case Keys::Up: case Keys::Right: case Keys::Left: case Keys::Space: e->IsInputKey = true; break; } – Divins Mathew Jun 01 '16 at 19:16
  • Edit your question then :) – Rakete1111 Jun 01 '16 at 19:18
  • Do you have something in keydown?? – Rakete1111 Jun 01 '16 at 19:21
  • yes there is..ive updated the question.. – Divins Mathew Jun 01 '16 at 19:36
  • You are using C++/CLI with WinForms. The code is almost ***identical*** to the C# code. The only thing you have to change from [this answer](http://stackoverflow.com/a/5606692/366904) is `Keys.Xxx` to `Keys::Xxx`. – Cody Gray - on strike Jun 17 '16 at 17:14

2 Answers2

0

Here is a C++ example of how to capture Key presses: C++ Detect when user presses arrow key

And here is the MSDN doc for the KeyDown event with some C++ code examples: https://msdn.microsoft.com/en-us/library/system.windows.forms.control.keydown(v=vs.110).aspx?cs-save-lang=1&cs-lang=cpp#code-snippet-1

hope that helps.

Community
  • 1
  • 1
Lucas C.
  • 59
  • 4
0

I managed to solve it.

For anyone facing the same, it worked when I added events PreviewKeydown and KeyDown events with the same code, for each of the controls in the form, but not for the form.

Divins Mathew
  • 2,908
  • 4
  • 22
  • 34