-
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!
-
Well, ASCII doesn't have arrow keys, so you're SOL. :p
http://faq.cprogramming.com/cgi-bin/...&id=1043284392
-
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
-
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?
-
Code:
POINT XY;
GetCursorPos( &XY );
:p
-
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);
-
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.
-
I was just using 10 pixels as an example. Thanks for the help.
-
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*
-
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?
-
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.
-
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;
}
-
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
do??
-
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...
-
Dev-C++ doesn't support kbhit(), as far as I remember.
-
Try using Borland's command-line compiler (in the second table on that linked page named "Compiler"). It is free and works very well, but you may not be too keen on it because the free version includes no IDE. Iow, you have to compile by typing a command line. It's actually the only compiler I use except when the situation requires otherwise.
Good luck to you.
-
>LuckY, what compiler do you use? (I use dev-c++) the code you gave me doesnt run...
Change cprintf to printf. It compiles fine under Dev-C++ after making that change.
-
Ok, now LuckY's program works, but it returns the same values as the number keys (the ones above the letters). I need to know how to tell when and what numpad keys are pressed. Im kind of in a rush, this is due on Friday...
-
That's only possible using GetAsyncKeyState, a function from windows.h. Or by having a full message loop, but then you can't use the Windows console anymore.
-
Im already using GetAsyncKeyState. How do I know when the numpad keys?
At this point im going to put up my code, so everyone can see what im doing. You don't have to take it and edit it and spit it back out. Infact, please don't. I would rather learn how to do it than have someone do it for me.
Code:
#include <iostream>
#include <stdio.h>
#include <conio.h>
#include <windows.h>
#define SHIFTED 0x8000
//define variables
int LEFT; int RIGHT; int UP; int DOWN;
int LSHIFT; int RSHIFT;
int ENTER;
int LALT; int RALT;
int PRESSED;
int scale;
int main ( void ){
//set arrow-key keycodes
LEFT=37; RIGHT=39; UP=38; DOWN=40;
//set shift keycodes
LSHIFT=160; RSHIFT=161;
//set enter keycode
ENTER=13;
//set alt keycodes
LALT=164; RALT=165;
//set key-pressed value
PRESSED=-32767;
//set default movement scale
scale=2;
//define cursor point
POINT XY;
//print instructions
cout<<"Use the arrow keys to move the cursor, hold SHIFT to move the cursor faster."<<endl;
cout<<"Press ENTER to left-click, and ALT to right-click."<<endl;
//main loop
while(1==1)
{
//if enter pressed then left-click
if(GetAsyncKeyState(ENTER)==PRESSED){
mouse_event(MOUSEEVENTF_LEFTDOWN, XY.x, XY.y, 0, 0);
Sleep(100);
mouse_event(MOUSEEVENTF_LEFTUP, XY.x, XY.y, 0, 0);
}
//if alt pressed then right-click
if(GetAsyncKeyState(LALT)==PRESSED||GetAsyncKeyState(RALT)==PRESSED){
mouse_event(MOUSEEVENTF_RIGHTDOWN, XY.x, XY.y, 0, 0);
Sleep(100);
mouse_event(MOUSEEVENTF_RIGHTUP, XY.x, XY.y, 0, 0);
}
//move mouse with arrow keys. set scale to 10 if shift is pressed, else 2
if(GetAsyncKeyState(LEFT)==PRESSED){
if(GetKeyState(LSHIFT) & SHIFTED||GetKeyState(RSHIFT) & SHIFTED){
scale=10;
}else{
scale=2;
}
GetCursorPos( &XY );
SetCursorPos( XY.x - scale, XY.y);
}
if(GetAsyncKeyState(RIGHT)==PRESSED){
if(GetKeyState(LSHIFT) & SHIFTED||GetKeyState(RSHIFT) & SHIFTED){
scale=10;
}else{
scale=2;
}
GetCursorPos( &XY );
SetCursorPos( XY.x + scale, XY.y);
}
if(GetAsyncKeyState(UP)==PRESSED){
if(GetKeyState(LSHIFT) & SHIFTED||GetKeyState(RSHIFT) & SHIFTED){
scale=10;
}else{
scale=2;
}
GetCursorPos( &XY );
SetCursorPos( XY.x, XY.y - scale );
}
if(GetAsyncKeyState(DOWN)==PRESSED){
if(GetKeyState(LSHIFT) & SHIFTED||GetKeyState(RSHIFT) & SHIFTED){
scale=10;
}else{
scale=2;
}
GetCursorPos( &XY );
SetCursorPos( XY.x, XY.y + scale );
}
}
return 0;
}
-
http://msdn.microsoft.com/library/de...alKeyCodes.asp
Lists all the valid parameters to GetAsyncKeyState. There are VK_NUMPADn constants there, too.