Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Key down events in a CView derived class

Posted on 2009-05-12
7
Medium Priority
?
593 Views
Last Modified: 2013-11-20
HIya

I have an SDI application. How can I capture the event when Ctrl + A, Tab + A or a key from the numeric keypad are pressed e.g. PgUp.

John
0
Comment
Question by:IssacJones
[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
  • 3
  • 3
7 Comments
 
LVL 31

Expert Comment

by:Zoppo
ID: 24362593
Hi IssacJones,

well, for simple keystrokes like 'Ctrl-A' easiest is to use the view's default command message handling by adding a new command for this keystroke in the application's accelerator table.

'Tab-A' is not a single keystroke, are you sure you need it and it's not a typo?

To distinguish between keystrokes on numeric keypad you directly have to handle WM_KEYDOWN message - unfortunateley this only can be done within a overridden PreTranslateMessage function since the view doesn't pass unhandled keystroke-messages to a WM_KEYDOWN message handler.

To do so override the PreTranslateMessage function of the view and implement it somehow like this:

> BOOL CTestViewSDIView::PreTranslateMessage(MSG* pMsg)
> {
>       if ( pMsg->message == WM_KEYDOWN )
>       {
>             if ( pMsg->wParam == VK_PRIOR )
>             {
>                  bool bNumPad = ( pMsg->lParam & ( 1 << 24 ) ) == 0; // see MSDN about WM_KEYDOWN for description of extended-key flag
>
>                  TRACE( "PageUp pressed (extended: %s)\n", false == bNumPad ? "false" : "true" );
>
>                  // don't pass it to the view's default message handler as long as you handle it yourself
>                  return TRUE;
>            }
>      }
>
>      return CView::PreTranslateMessage(pMsg);
> }

Hope this helps,

ZOPPO
0
 
LVL 19

Assisted Solution

by:alb66
alb66 earned 100 total points
ID: 24363673
>>>> 'Tab-A' is not a single keystroke
Zoopo is right. But if you need that for your reason, may be you can handle WM_KEYDOWN for key A, and test the Tab state via GetAsyncKeyState().
0
 

Author Comment

by:IssacJones
ID: 24363789
Hi Zoppo

Many thanks for your help. I didn't explain the "single keystroke" very well. What I was planning to trap was when a user presses Tab and A simultaneously e.g. in a similar manner in Visual Studio you can press Ctrl and Tab at the same time to cycle through files.

Indeed, what I want to do with trapping Tab and A at the same time is to cycle through some views. I thought I'd worked out how to do this but now I suspect I need help with this as well. There are two views that I want to cycle through. Both views are in two seperate functions which I'm going to call  in OnDraw.

Any ideas on how to do it?

John
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 31

Expert Comment

by:Zoppo
ID: 24363858
Hi again,

well, IMo it's quite unusual to combine two keys for a command - CTRL, SHIFT and ALT are treated as some kind of state-modifiers for a pressed key, TAB and A are treated as single keystrokes.

Anyway, it can be done either the way alb66 suggested - see attached code for this ...

ZOPPO


BOOL CTestViewSDIView::PreTranslateMessage(MSG* pMsg)
{
	if ( pMsg->message == WM_KEYDOWN )
	{
		if ( pMsg->wParam == VK_PRIOR )
		{
			bool bNumPad = ( pMsg->lParam & ( 1 << 24 ) ) == 0; // see MSDN about WM_KEYDOWN for description of extended-key flag
 
			TRACE( "PageUp pressed (extended: %s)\n", false == bNumPad ? "false" : "true" );
		}
		else if ( pMsg->wParam == 'A' )
		{
			if ( GetKeyState( VK_TAB ) < 0 )
			{
				TRACE( "A+TAB pressed ...\n" );
			}
		}
 
		// don't pass it to the view's default message handler
		return TRUE;
	}
 
	return CView::PreTranslateMessage(pMsg);
}

Open in new window

0
 

Author Comment

by:IssacJones
ID: 24364019
Hi Zoppo

Ah, okay, could I bother you with one final thing, what would be the best way to deal with Ctrl and Tab i.e. the same as the example I gave with Visual Studio?

John
0
 
LVL 31

Accepted Solution

by:
Zoppo earned 400 total points
ID: 24364142
No problem - For Ctrl-TAB accelerators even don't work (since I guess it is somehow handled especially by MFC), but you can do it the same way:

> ...
> else if ( pMsg->wParam == VK_TAB )
> {
>  if ( GetKeyState( VK_CONTROL ) < 0 )
>  {
>   TRACE( "Ctrl+TAB pressed ...\n" );
>  }
> }
> ...

ZOPPO
0
 

Author Comment

by:IssacJones
ID: 24364166
Excellent! I'll allocate points to both of you because I learnt something new from you both.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

604 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