Solved

Consume Keystroke

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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

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…
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…
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.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

728 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