Thread: Ascii

  1. #1
    Registered User Bag a Bones's Avatar
    Join Date
    Dec 2005
    Posts
    15

    Ascii

    Im working on a final project for grade 11 and I need to know how to use ASCII. I've looked for tutorials and other information, but its too complicated for me. Basicaly what I need to do is have an event happen when the arow keys are pressed. I don't know the syntax (or even the name of the function for that matter) to see what key has been pressed. Im hoping to have case statemens so for instance if the ascii code was 77 it would be

    Code:
    //This is probably more like pseudo code than anything...
    
    int x,y;
    
    switch (KeyCode)
    {
    case 77:
       x ++;
       y ++;
       SetCursorPos(x,y);
    }
    This is probably a false hope, but still... Any help or resources would be greatly appreciated!

    Thanks!

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Well, ASCII doesn't have arrow keys, so you're SOL.
    http://faq.cprogramming.com/cgi-bin/...&id=1043284392
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    8

    maybe this will help

    I'm not sure what os your writing for but you would either have to use an os API or use a library that does it for you. At least to the best of my knowledge.
    Code:
    SHORT GetAsyncKeyState(      
    
        int vKey
    );
    Take a look at the following pages
    Windows keyboard input
    Keycodes
    Last edited by disks86; 12-22-2005 at 02:15 PM.

  4. #4
    Registered User Bag a Bones's Avatar
    Join Date
    Dec 2005
    Posts
    15
    Thanks very much! I now have the keyboard part done. Now I need a function to find out where the mousecursor currently is. I think its GetCursorPosition, but I dont know how to use it properly. If its like SetCursorPos I should be fine. Can anyone help me with that?

  5. #5
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269
    Code:
    POINT XY;
    GetCursorPos( &XY );
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  6. #6
    Registered User Bag a Bones's Avatar
    Join Date
    Dec 2005
    Posts
    15
    So if i wanted to move the cursor 10 pixels up it would be:

    Code:
    int x,y;
    
    POINT XY;
    GetCursorPos( &XY );
    y = Y + 10;
    SetCursorPos(x,y);

  7. #7
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Maybe something more like:

    Code:
    	POINT XY;
    
    	GetCursorPos( &XY );
    	SetCursorPos( XY.x, XY.y - 10 );
    10 pixels is hardly noticable though, you might not see it.

  8. #8
    Registered User Bag a Bones's Avatar
    Join Date
    Dec 2005
    Posts
    15
    I was just using 10 pixels as an example. Thanks for the help.

  9. #9
    Cat Lover
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109
    Actually, you can get ascii keys for their function.

    Code:
    //user presses arrow key
    cin.get(); //returns 77
    cin.get(); //returns different value depending on key, tab, up arrow, etc
    Somewhere else in this forum I think it was, someone had a similar problem.

    *edit*spelling*

  10. #10
    Registered User Bag a Bones's Avatar
    Join Date
    Dec 2005
    Posts
    15
    Ok, iv decided to change it from the arrow keys to the numpad, just like the Windows Version of MouseKeys. I can't seem to find the ascii values for the numpad though. Does anyone know what they are?

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    They don't have individual ASCII values. The numbers just have the usual digit values, while the others (numlock off) ... I have no idea how, if at all, they're reported.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  12. #12
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    I think a brief explanation of ASCII is warranted here. You, Bag a Bones, seem to be confusing keyboard values for ASCII values. What you must keep in mind is that ASCII is simply a standard that specifies what character each number in the range 0-127 represents. Each individual key on a keyboard does not have an ASCII value that corresponds with it, but there are some keypresses that have and generate a corresponding ASCII code. For example, pressing TAB will generate a 9, but pressing F1 does not generate an F and a 1.

    What you should do, for a learning experience at the very least, is use conio.h to see the values that are generated when you press different keys. Run this code and hit random keys. You'll discover that non-ASCII values are unique (and some keys generate no keypresses).
    Code:
    #include <iostream>
    #include <conio.h>
    using namespace std;
    
    ////////////////////////////////////////////////////////////////////////////////
    
    const int ESCAPE = 27;
    
    int main() {
      char ch = 0;
      
      while (ch != ESCAPE) {
        if (kbhit()) {
          while (kbhit()) {
            ch = getch();
            cprintf("%c=%d\r\n", ch, ch);
          }
        }
      }
      
      return 0;
    }
    Last edited by LuckY; 01-10-2006 at 10:57 AM.

  13. #13
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    or if you want it printed in hex,

    Code:
    #include <iostream>
    #include <conio.h>
    using namespace std;
    
    ////////////////////////////////////////////////////////////////////////////////
    
    const int ESCAPE = 27;
    
    int main() 
    {
    	char ch = 0;
    
    	while (ch != ESCAPE) 
    	{
    		if (kbhit()) 
    		{
    			while (kbhit()) 
    			{
    				ch = getch();
    				cprintf("%c=%0x\r\n", ch, ch);
    			}
    		}
    	}
    	return 0;
    }
    PS - cool code!!!


    PS, what does

    Code:
    kbhit
    do??
    Last edited by twomers; 01-10-2006 at 12:26 PM.

  14. #14
    Registered User Bag a Bones's Avatar
    Join Date
    Dec 2005
    Posts
    15
    LuckY, thanks, your diagnosis is quite correct. I thought that every key had an ASCII value. Thanks for setting me straight.

    twomers, kbhit checks the keyboard buffer to see if a key has recently been hit. The getch() function can be used to find what key has been hit.

    LuckY, what compiler do you use? (I use dev-c++) the code you gave me doesnt run...

  15. #15
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Dev-C++ doesn't support kbhit(), as far as I remember.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ASCII character with ASCII value 0 and 32
    By hitesh_best in forum C Programming
    Replies: 4
    Last Post: 07-24-2007, 09:45 AM
  2. Replies: 11
    Last Post: 03-24-2006, 11:26 AM
  3. Office access in C/C++ NOT VC++!! :)
    By skawky in forum C++ Programming
    Replies: 1
    Last Post: 05-26-2005, 01:43 PM
  4. ascii values for keys
    By acid45 in forum C Programming
    Replies: 2
    Last Post: 05-12-2003, 07:13 AM
  5. Checking ascii values of char input
    By yank in forum C Programming
    Replies: 2
    Last Post: 04-29-2003, 07:49 AM