Solved

Consume Keystroke

Posted on 2004-10-02
5
355 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

759 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now