Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 5th, 2007, 2:08 AM   #1
ZenOswyn
Newbie
 
Join Date: Nov 2006
Posts: 5
Rep Power: 0 ZenOswyn is on a distinguished road
Need help with user input

Hey all,
I'm making a small console game, and I'm looking for another way to get users input. I'm using the WASD keys to move a cursor on the screen, but the only way I know to get a users input is by using cin. As you can guess, having to press a direction key and then hit Enter is a bit time consuming on the users side and I was wondering if anyone can give me some help in finding another way to do this.

I know this can be accomplished using ncurses, and probably other libraries. But I'd really like to stick with the standard C++ libraries if possible.

Oswyn
ZenOswyn is offline   Reply With Quote
Old May 5th, 2007, 2:42 AM   #2
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,198
Rep Power: 5 grumpy is on a distinguished road
Not possible. There is no way in the standard C or C++ libraries to receive individual keystrokes. That's because the standard libraries are based around a pretty generic computing device (and, generically, a computing device may not have a keyboard).

If you are targeting a machine with a keyboard, most compilers you will be using and their libraries include functions that provide some means of obtaining keystrokes directly. You need to read your compiler and library documentation to find out how to do it. Keep in mind that the method varies between compilers and systems. If you want to use curses or similar, look up raw input.
grumpy is offline   Reply With Quote
Old May 5th, 2007, 4:11 AM   #3
ZenOswyn
Newbie
 
Join Date: Nov 2006
Posts: 5
Rep Power: 0 ZenOswyn is on a distinguished road
I was afraid that would be the case. I looked over some curses examples and came up with this, a pretty minimal solution, in case anyone is interested.

#include <ncurses.h>

int main() {
	initscr();
	raw();
	noecho();
	
	char ch;
   
	while(!ch || ch != 'x') {
		ch = getch();
		printw("You pressed: %c \n", ch);
	}

	return 0;
}
ZenOswyn is offline   Reply With Quote
Old May 5th, 2007, 10:17 AM   #4
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,198
Rep Power: 5 grumpy is on a distinguished road
Quote:
Originally Posted by ZenOswyn View Post
    char ch;
   
    while(!ch || ch != 'x') {
Technically, the above yields undefined behaviour (reading the value of an uninitialised variable yields undefined behaviour). To avoid that, initialise ch to zero (or to some value not equal to 'x') before the loop.
grumpy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
User input jayme C++ 38 Nov 25th, 2005 7:27 PM
FiveDigit + RandomeNumber Game. TecBrain Java 0 Nov 18th, 2005 2:53 PM
User Input for Number Format ericelysia1 Java 0 Jul 21st, 2005 3:41 PM
Problem read'ing from file and user input jmsilver Bash / Shell Scripting 3 May 20th, 2005 2:35 PM
Instant Messaging App Help AusTex C 0 Apr 27th, 2005 4:52 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:46 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC