Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

VIsual Studio 2005 C++ arrowkey capture, help me make more progress in my console wordprocessor

Posted on 2007-07-30
2
Medium Priority
?
566 Views
Last Modified: 2013-12-14
I use Visual Studio 2005 and I have have trouble capturing the arrow keys on my console word processor project.  I got a far a capturing them but when I press shift + H it thinks I am pressing the up key.  Why when the up key is clearly \0H  (null H)  and not shift H> wierd.  Maybe I need to use a string and not a char to store the keypresses.

Here a code fragment:

if (_kbhit())
{

      keyPress = _getch();
      if (keyPress != '\0')
      {

            if (keyPress >= 32 && keyPress <= 127)
            {                  
                  if (keyPress == '\0H') //up key pressed
                  {
                        if (arrayIndex != 0) // if not a TOP
                        {
                        y--; arrayIndex--;
                        if (x > m.len(line,arrayIndex) + (boxX +1))
                        {
                                                      
                        x = (m.len(line,arrayIndex) + (boxX +1));
                        stringIndex = m.len(line,arrayIndex);
                        }
                              m.gotoxy(x,y);
                        }
                  }
                  else if (keyPress == '\0P') //down
                  {
                                          
                  }
                  else if (keyPress == '\0M') //right
                  {
                                          
                  }
                  else if (keyPress == '\0K') //left
                  {
                                          
                  }
                  else // not an arrow key (implmented) of function key (not implmented yet)
0
Comment
Question by:jim_pugh
2 Comments
 
LVL 11

Accepted Solution

by:
Jase-Coder earned 750 total points
ID: 19596909
hi,

you have to call getch twice. first to get the special character, which is the shift key and twice to read the actual key that was presssed:
check http://support.microsoft.com/kb/57888

for example

         if (keyPress >= 32 && keyPress <= 127)
            {                  
                  if (keyPress == 0x00) //up key pressed
                  {
                       keypress = getch()
                       if(keypress == 'H')
                      {
                           if (arrayIndex != 0) // if not a TOP
                          {
                                     y--; arrayIndex--;
                                    if (x > m.len(line,arrayIndex) + (boxX +1))
                                    {
                                    }
                           }
                        }
0
 

Author Comment

by:jim_pugh
ID: 19599360
Cheers mate.  That link sorted me out.  It is best to capture the key press as an int that stores is ASCII value rather than a char that stores the letter.  Then when I cout the int, I just do it likes this
cout << char(int) ;
rather than cout << char;
and save it to my array the same way.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question