0

So I want a function to return the char representing an arrow key to use it further in the main, and here's my try:

#include <conio.h>
#include <iostream>
using namespace std;

#define KEY_UP 72
#define KEY_DOWN 80
#define KEY_LEFT 75
#define KEY_RIGHT 77

char func()
{
    char c;
        c=_getch();
        switch((c=_getch())) {
        case KEY_UP:
            return c;
        case KEY_DOWN:
            return c;
        case KEY_LEFT:
            return c;
        case KEY_RIGHT:
            return c;
        default:
            break;
        }
        return 0;
}
int main()
{
    cout<<func();
}

But it doesn't work , it just prints the character with the 'actual' ASCII value (like 'M' if I pressed the right arrow key) , So i want to know how to solve this problem . Also why is it that the same ASCII value is used for a character as well as the arrow key? What does it mean?

Another question is that modern IDE's don't use getch() or _getch() functions , so what is the alternate of that?

  • *is that modern IDE's don't use getch() or _getch() functions* - The compiler matters, not IDEs, because they use compiler for building. – Rohan Bari Jun 06 '20 at 02:03
  • Could you elaborate on what you expect it to print? – Mestkon Jun 06 '20 at 02:15
  • 1
    If you use `getch()`, it'll return `0` every time you enter arrow keys. To prove it simply, you just need to change the return type of `fun()`, change that into `int` and try again. – Rohan Bari Jun 06 '20 at 02:16
  • *"the char representing an arrow key"* -- there is no such character, so your plan is doomed to failure. On the other hand, [XY problem](https://en.wikipedia.org/wiki/XY_problem)? What are you really trying to accomplish? What's the end goal? – JaMiT Jun 06 '20 at 02:22
  • @JaMiT what i was trying to do is to create a function , which would input an arrow key , and then return it so that i can use it for future , but I understood now , I can just use ASCII value of 'M', which is returned to denote the 'right arrow key' and similarly others But i still don't understand this concept of getch() returning different things when called different number of times , So if anyone could give me a reference? Also , what is the alternate of _getch() ? – PRAKHAR SINGH Jun 06 '20 at 02:34
  • @RohanBari Understood now Thanks! – PRAKHAR SINGH Jun 06 '20 at 02:40
  • Does this answer your question? [C++ Detect when user presses arrow key](https://stackoverflow.com/questions/24708700/c-detect-when-user-presses-arrow-key) – Rohan Bari Jun 06 '20 at 02:46
  • @PRAKHARSINGH You can return a value signifying an arrow key. It just won't be a `char` if you want to track all keys. There is a huge difference between "recording that up arrow was pressed" and "returning the char representing an arrow key". When your plan runs into an obstacle, keep at least one eye on the overall goal, and when you ask for help here, be sure to describe the overall goal just in case your plan is fundamentally flawed. – JaMiT Jun 06 '20 at 03:47
  • @PRAKHARSINGH The `_getch()` function (as opposed to the one without the underscore) is a Microsoft extension, so a good place to look for a reference is [Microsoft Docs](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/getch-getwch?view=vs-2019). Maybe you can update your question after reading through that? Give a better description of what your real goal is and how you see `_getch()` fitting in? – JaMiT Jun 06 '20 at 03:50

0 Answers0