Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Need help with a C programming assignment!

+4 votes
asked Jun 29, 2020 by Iftiaq (620 points)
You should finish a program which can respond to the keyboard.(That means if you press UP key , the screen could display the words” UP key is pressed!”)The keys which you should finish include ‘a’、’w’、’d’、’s’ and four direction keys.

1 Answer

+4 votes
answered Jun 30, 2020 by Peter Minarik (84,720 points)
selected Jun 30, 2020 by Iftiaq
 
Best answer

I'd suggest looking at the documentation.

Using the standard C library you can easily create a program that reads characters until the user hits ENTER then processes them one by one. Have a look at getchar(), which exactly does that: reads input (sequence of characters) until the user presses ENTER.

Unfortunately, the C standard does not support checking if a key was hit, hence the above implementation (reading keys first, processing them later) is easy. For processing keys one by one, you need some platform specific libraries, such as

In general, it's a useful skill to be able to search the Internet and find what you're looking for.

With these functions you should be able to complete your assignment. And remember to keep checking the documentation. :)

One more link, that is useful from someone else's question on StackOverflow: https://stackoverflow.com/questions/20349585/c-library-function-to-check-the-keypress-from-keyboard-in-linux

Good luck!

commented Jun 30, 2020 by Iftiaq (620 points)
edited Jun 30, 2020 by Iftiaq
#include <stdio.h>
#include <conio.h>
#define Up_key 0x48
#define Right_key 0x4D
#define Left_key 0x4B
#define Down_key 0x50
void main()
{
    unsigned char c;
    
        c = _getch();
        if (c == 0xe0 || c == 0)
        {
            c = _getch();
            switch (c)
            {
            case Up_key:printf("Up is pressed\n");
                break;
            case Down_key:printf("Down is pressed\n");
                break;
            case Left_key:printf("Left is pressed\n");
                break;
            case Right_key:printf("Right is pressed\n");
                break;
            }
        }
        else
        {
            switch (c)
            {
            case 'a':printf("a is pressed\n");
                break;
            case 's':printf("s is pressed\n");
                break;
            case 'd':printf("d is pressed\n");
                break;
            case 'w':printf("w is pressed\n");
                break;
            }
        }
       

}
thank you, i came up with this code :)
commented Jul 1, 2020 by Peter Minarik (84,720 points)
It looks OK to me. I emphasize *looks* as this code targets Windows platform, so it doesn't run under OnlineGDB.

The logic is clear. The code seems to work.

Here are a few improvement suggestions:
1. maybe add a default: to the switch and say "unknown key". It makes it easier to find what happened if you pressed a key, you expected something to happen, but nothing is output.
2. I suspect the Up/Down/Left/Right key codes are already defined. If you can find which header file has them (search the web), you can include that header so you don't need to create defines for something that already exists.

So yeah, looks fine.

Good job! :)
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...