Solved

Consume Keystroke

Posted on 2004-10-02
5
357 Views
Last Modified: 2010-07-27
Hi,

VC++6 Windows:

I need some help on, how can I consume keystrokes.
Say when the user presses a Tab key if the user has pressed some other or Tab key (either holding it) and I am in the process of processing first keystroke. I want to destroy all keystoks that the user may have triggered when first key was being processed.

Many thanks
0
Comment
Question by:msjammu
  • 4
5 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12206958
That will depend what kind of applications are you building: console, Win32, MFC. But a generica aproach is something like this:

c = getch();   // Wait to read first keystroke
// some processing
while (getch()) {}  // Read (comsume) all keystrokes triggered
// some other processing

0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12206970
Sorry, maybe not a good example. Try with this:

// this header is needed to use getch() and kbhit()
#include "conio.h"
 
int main ()
{
    // some code here

int c;
c = getch();   // Wait to read first keystroke
// some processing
while (kbhit())  // verify if key exist in buffer
      { getch(); }  // Read (comsume) all keystrokes triggered
// some other processing

    return 0;
}
0
 
LVL 6

Author Comment

by:msjammu
ID: 12207010
>>VC++6 Windows:
My application is windows application MDIBased;
containing many child forms
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12207036
There are many tecniques, also you can control the PreTranslateMessage event in your application.
You can filter all the WM_CHAR messages is a flag is on

Create a flag member in your CWinApp derived object:
       bool m_IgnoreKeys;

Then create an event with the ClassWizard (Ctrl-W) for your app object, and control WM_CHAR specific message

(inside CYourApp::PreTranslateMessage)
   if (msg==WM_CHAR) {
       if (!m_IgnoreKeys)        // if m_IgnoreKeys = false
            return FALSE;          // Do nothing
   }
// else continue with pretranslatemessage


0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 500 total points
ID: 12207060
Sorry, must be 'return TRUE' to "swalow" the message. Will give you a more concrete example:

BOOL CTest55App::PreTranslateMessage(MSG* pMsg)
{
      if (pMsg->message==WM_CHAR && m_IgnoreKeys) {
            ::MessageBeep(MB_OK);     // Just for testing
            return TRUE;
      }
      // TODO: Add your specialized code here and/or call the base class
      
      return CWinApp::PreTranslateMessage(pMsg);
}

Now you decide when keys will be accepted by setting  the m_IgnoreKeys to true or false
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

803 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