Thread: key detection with SDL library

  1. #1
    coder
    Join Date
    Feb 2008
    Posts
    127

    key detection with SDL library

    hello

    I am making a chat program which (obviously) needs to detect the ascii of the key pressed.

    Unfortunately I can get only the ascii of the "real" key pressed.
    I don't know how to explain that, so here's an example:

    when the user presses just <a>, SDL gives me the code 97 (which is the correct ascii code for "a")
    if the user presses <SHIFT + a>, SDL again gives me 303 (the R-SHIFT code used by SDL) and again 97.

    ok, I simply might recognize when the user presses shift, which is not a hard deal but...
    if I have to check for non-alphabetical keys?
    here is the complication, explained with a comparation:

    italian user presses <9> : ascii code is 57
    english user presses <9> : ascii code is 57

    italian user presses <SHIFT + 9> : character is ")", ascii code is 41
    english user presses <SHIFT + 9> : character is "(", ascii code is 40

    I've already tried to implement a function which detects the input, but there happen some conflicts with SDL.
    also my knowledge with low-level C coding is not good.

    Which way should I start from? thanks

    edit: I'd also be glad to make it portable

    edit solved: ok, nevermind, I've just found the perfect solution:
    http://listas.apesol.org/pipermail/s...ne/044058.html
    Last edited by carlorfeo; 02-07-2008 at 08:40 AM. Reason: solved

  2. #2
    coder
    Join Date
    Feb 2008
    Posts
    127
    This is the code (found somewhere in the net) from which I've extracted a function to be used by my program.
    Even studying it, I still don't understand the great part of these lines...
    By the way I guess this program deals with the terminal specifically, which is not what I really need, am I wrong?

    Code:
    #include <termios.h>
    #include <iostream>
    
    int main(void)
    {
    	int count;
    	int jndex;
    	int result;
    	int virgin;
    
    	char echoed_data;
    	char in_buffer[80];
    
    	struct termios tp1;
    	struct termios tp2;
    
    	virgin=1;
    	echoed_data='X';
    
    	printf("Type a space character to exit.\n");
    
    	tcgetattr(0,&tp1);
    
    	tp2=tp1;
    
    	tp2.c_iflag&=~ICRNL;
    	tp2.c_lflag&=~ICANON;
    	tp2.c_lflag&=~ECHO;
    	tp2.c_cc[VMIN ]=1;
    	tp2.c_cc[VTIME]=0;
    	tp2.c_cc[VINTR]=0xFF;
    	tp2.c_cc[VSUSP]=0xFF;
    	tp2.c_cc[VQUIT]=0xFF;
    
    	tcsetattr(0,TCSANOW,&tp2);
    
    	do {
    		in_buffer[0]=0;
    
    		count=read(0,in_buffer,1);
    
    		if(virgin)
    		{
    			virgin=0;
    			tp2.c_cc[VMIN]=0;
    			tcsetattr(0,TCSANOW,&tp2);
    		}
    
    		if(count>0)
    			echoed_data=in_buffer[0];
    
    		printf("&#37;d: ", echoed_data);
    		printf("%d\n", 256 + echoed_data);
    
    		fflush(stdout);
    
    	} while(echoed_data!=0x20);
    
    	printf("\n");
    
    	tcsetattr(0,TCSANOW,&tp1);
    
    	return 0;
    
    }
    Last edited by carlorfeo; 02-07-2008 at 07:55 AM.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You probably want to use SDL events, and in particular SDL KeyboardEvent

    Search the web for those two, and you will find more info.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    coder
    Join Date
    Feb 2008
    Posts
    127
    Thank you matsp
    That's it. I didn't search enough

    Quote Originally Posted by carlorfeo
    edit solved: ok, nevermind, I've just found the perfect solution:
    http://listas.apesol.org/pipermail/s...ne/044058.html

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  2. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  3. SDL multiple key presses...Help.
    By SyntaxBubble in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2003, 11:52 AM
  4. key detection
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2001, 09:48 AM
  5. BST/Red and Black Tree
    By ghettoman in forum C++ Programming
    Replies: 0
    Last Post: 10-24-2001, 10:45 PM