-3

Im making a menu. I want to use arrow keys to select from my list.

char move;

do
{
    move = (char)_getch();
    if (move == 38)
    {
         // Move Indicator Up   
    }
    else if (move == 40)
    {
         // Move Indicator Down
    }
}
while (move != 13);

Am i using the wrong ascii values for up and down keys?

SOLVED

i replaced (char)_getch() to (int)_getch() and char move to int move then 38 and 40 to ?? and 80

Machavity
  • 30,841
  • 27
  • 92
  • 100
Reinan Contawi
  • 225
  • 1
  • 6
  • 13
  • what is _getch() ? I see C# like a tag. – Tigran Jul 19 '11 at 11:29
  • 1
    Are you sure this is C# code? Based on your call to `_getch`, it looks more like C. – Joe White Jul 19 '11 at 11:30
  • it's c#. I tried replacing 38 with 'w' and it worked. – Reinan Contawi Jul 19 '11 at 11:33
  • Are you P/Invoking `_getch()` from `msvcrt.dll`? And if so, **why**? – Cody Gray - on strike Jul 19 '11 at 11:40
  • i dont now why. i used it in my previous maze game and it works properly. i;m just having a hard time when it comes to arrow keys. – Reinan Contawi Jul 19 '11 at 11:46
  • 1
    Instead of marking the question text as "SOLVED" you should accept an answer. – K Mehta Jul 19 '11 at 11:47
  • accept an answer!? I didn't get the answer I want yet. not now. :)) – Reinan Contawi Jul 19 '11 at 11:47
  • both answers underneath seem to work :) Are they not what you're looking for? Unless you have a very good reason to, you shouldn't pinvoke c functions into c# if there is a c# counterpart for it already. – K Mehta Jul 19 '11 at 11:50
  • It's solved now. 80 is for down and 72 is for up. Thanks for the effort guys. I'll choose an answer still. – Reinan Contawi Jul 19 '11 at 11:50
  • 1
    Uh, you should be using `Console.Read` or `Console.ReadKey`. P/Invoking `_getch()` is definitely the wrong solution. Check [here](http://msdn.microsoft.com/en-us/library/ms395663) for C# equivalents to CRT functions. – Cody Gray - on strike Jul 19 '11 at 11:57
  • I would have to agree with Cody there – K Mehta Jul 19 '11 at 13:24
  • 4
    @ReinanContawi: If no answer was correct because your question was too trivial or too localized to your particular issue, please try to solve it yourself. Asking a question for every tiny incremental change you make to your code is considered [help vampirism](http://slash7.com/2006/12/22/vampires/) and will result in your account being blocked from asking questions. –  Jul 19 '11 at 15:51

2 Answers2

6

Seems like you're DllImporting msvcrt.dll to use _getch()

Try using Console.ReadKey()

ConsoleKeyInfo keyInfo = Console.ReadKey();
if (keyInfo.Key == ConsoleKey.UpArrow) {

} else if (keyInfo.Key == ConsoleKey.DownArrow) {

} ...
K Mehta
  • 10,323
  • 4
  • 46
  • 76
1

In case we are talking about a WinForms application i would recommend you to use the Control.KeyDown Event. "Console.Read()" doesn't work for WinForms applications.

Update Example for menu navigation with arrow key for console application in C#. >> Sample 1 Sample 2

Community
  • 1
  • 1
Alexander
  • 3,724
  • 8
  • 42
  • 50