954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Can I make my C program understand that a key has been pressed?

Hello there, guys!

I am quite some time now into programming, but have been dealing with C source codes for a short time. After creating some very basic programs, I want to do something more "sophisticated", might I use this expression. But let me not beat around the bush and get to the point.

I want to make a Tic-Tac-Toe game (that is, I'm almost done with it), but in order to play, one needs to enter the co-ordinates of the cell one wants to play in. That is, if one wanted to put his move into the upper left corner, one would need to type in 1 1 and press ENTER.
What I want to do is make the program "perceive" the keystrokes of the user, and deal with them accordingly. In the above instance, the only thing one would need to do, should I know how to do it, would be to press the 7 on the numpad.

My question is, do I have this option in C, or will I need to proceed to learning C++? And if I can make it in C, in which way exactly?

Any help will be appreciated. Thank you for reading this post!

Shadowhawk
Newbie Poster
18 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 

What is your operating system? Are you trying to write a Console Application or a GUI Application?

WolfPack
Postaholic
Moderator
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
 

it can be done easily, though the libraries needed are pretty much platform specific as they depend on operating system calls which themselves rely on hardware calls.

Basically you have a loop which polls the keyboard (or waits for input from the keyboard) and quits when input is received (or calls a function when input is received depending on that input).

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

I am running Windows XP Pro English SP2, and I am trying to make a Console Application using DevC++ (it's the latest edition, beta).

Now, about the loop, I came to think that much. The problem is, to receive a message from the keyboard, the ENTER must be pressed, with the things I know so far. That's why I am actually looking for the function that receives a message without the ENTER being pressed.

By the way, a "beta" edition of the Tic-Tac-Toe is ready... If anyone wants to see it, you can get the zipped file here... Just when you run it, maximize the window so it looks the way I intended...

Shadowhawk
Newbie Poster
18 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 

Could you please post the code? I usually dont like running other people's exe files.

WolfPack
Postaholic
Moderator
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You