Solved

Consume Keystroke

Posted on 2004-10-02
5
360 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

734 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