Solved

processing key combinations

Posted on 2001-06-25
10
256 Views
Last Modified: 2013-12-03
Hi guys. What's the best way for me to process Ctrl + VK_UP or Vtrl + VK_DOWN messages within a rich edit box? If a hook is in order, please show me the way.

Thanks

Senkwe
0
Comment
Question by:chandas
[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
  • 3
  • 2
  • +1
10 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 6224715
You can check the state of any key at any time using the 'GetKeyState()' function. This allows you to check any combination of keys cunsecutively. If you want to check a combination of keys using a single API call, you can accomplish taht using 'GetKeyboardState()'
0
 
LVL 5

Expert Comment

by:robpitt
ID: 6225457
you can use a Keyboard Accelerator to map keypresses to wm_command messages.

To do this define an accelerator resource then load it with LoadAccelerator(). Now just add a call to TranslateAccelerator() in your main message loop, only if this returns false should you call TranslateMessage/DispatchMessage.

Search MSDN for "Keyboard Accelerators" for more info.

0
 

Expert Comment

by:yariv_e
ID: 6226435
Try to filter the messages in PreTranslateMessage.
in this example COptionsPage2 inherits from CPropertyPage.
PreTranslateMessage is called for the application as well as for the relevant CWnd.

BOOL COptionsPage2::PreTranslateMessage(MSG* pMsg)
{
   int msg = pMsg->message;
   if (msg == WM_KEYDOWN || msg == WM_KEYDOWN || msg == WM_SYSKEYDOWN) {
      //check if ctrl and what key was up or down
   }
   return CPropertyPage::PreTranslateMessage(pMsg);
}
0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

Author Comment

by:chandas
ID: 6226959
Rob, thanks for the response. That looks like it would work for something other than my problem. I can't map key presses to a WM_COMMAND because all I want to do is move the cursor to a certain position in the rich edit box depending on the CTRL + VK_UP or DOWN combinations (via SetSel() for example) By the way, since it's a rich edit control, I'm effectively overriding the controls default behaviour.

Yarive...your second posting looks intriguing. I'm not sure I understand your "if" statement though. Not only that, I'm not sure that CTRL is a system key. I've tried to break in my code at the point when CTRL is pressed (in my OnSysKeyDown(...) method) and it only seems to recognise the ALT key.

The GetKeyboardState method actually works, just seems kludgy because I have to check in WM_KEYUP. By that time, if you've pressed the arrow key, WM_KEYDOWN will have been processed already and so the caret will move first before I check the state of the CTRL key. It's not bad, happens quite fast...but just doesn't feel right. I'll stick with that if it's the only way to go.

I actually thought setting a hook would be the best way. It probably is but I don't understand the code well.

0
 

Author Comment

by:chandas
ID: 6227136
Rob, thanks for the response. That looks like it would work for something other than my problem. I can't map key presses to a WM_COMMAND because all I want to do is move the cursor to a certain position in the rich edit box depending on the CTRL + VK_UP or DOWN combinations (via SetSel() for example) By the way, since it's a rich edit control, I'm effectively overriding the controls default behaviour.

Yarive...your second posting looks intriguing. I'm not sure I understand your "if" statement though. Not only that, I'm not sure that CTRL is a system key. I've tried to break in my code at the point when CTRL is pressed (in my OnSysKeyDown(...) method) and it only seems to recognise the ALT key.

The GetKeyboardState method actually works, just seems kludgy because I have to check in WM_KEYUP. By that time, if you've pressed the arrow key, WM_KEYDOWN will have been processed already and so the caret will move first before I check the state of the CTRL key. It's not bad, happens quite fast...but just doesn't feel right. I'll stick with that if it's the only way to go.

I actually thought setting a hook would be the best way. It probably is but I don't understand the code well.

0
 
LVL 5

Expert Comment

by:robpitt
ID: 6228425
Personally I reckon you could still go allong with the keyboard accelerator trick, you would bind Ctrl+Up to a unique command value and then upon recieving that unique command value message you do your SetSel() or whatever.

An alternative mechanism would be to subclass the entire control and intercept the WM_KEYUP/KEYDOWN messages.

Rob

BTW, what language/toolset are you using?
C/C++/WTL/MFC?
0
 

Author Comment

by:chandas
ID: 6231256
Hi Rob

I'm currently trying to wrap my head around WTL. Your keyboard accelerator trick sounds great. It seems though, that I have a bit of reading to do regarding that "unique command value" of yours.

I am actually subclassing the control and intercepting WM_KEYUP etc. Only problem is these messages refer to one key press, not two. WM_SYSKEYUP works great BUT...for some very strange reason, when I then use SetSel(...) and type a single character at the new caret position, it always prints out one of those funny ascii characters (typically a small square) I don't even WANT to get into how that could even possibly be happening. I'll take a kludge anyday if it saves me from days of traversing the innards of the Win32 API.

Thanks for all the help guys.

Senkwe
0
 

Author Comment

by:chandas
ID: 6231632
Hi Rob

I'm currently trying to wrap my head around WTL. Your keyboard accelerator trick sounds great. It seems though, that I have a bit of reading to do regarding that "unique command value" of yours.

I am actually subclassing the control and intercepting WM_KEYUP etc. Only problem is these messages refer to one key press, not two. WM_SYSKEYUP works great BUT...for some very strange reason, when I then use SetSel(...) and type a single character at the new caret position, it always prints out one of those funny ascii characters (typically a small square) I don't even WANT to get into how that could even possibly be happening. I'll take a kludge anyday if it saves me from days of traversing the innards of the Win32 API.

Thanks for all the help guys.

Senkwe
0
 
LVL 5

Accepted Solution

by:
robpitt earned 150 total points
ID: 6232069
Since you've already subclassing the control, you should be able to just cacth WM_KEYDOWN and check the state of the Ctrl via GetKeyState(VK_CONTROL)<0. Then either handle the keypress or pass it on to the default richedit procedure.

Rob

PS
By "unique command value" I just meant a command ID in the same sense as ID_FILE_EXIT etc.

0
 
LVL 86

Expert Comment

by:jkr
ID: 6241056
>>and check the state of the Ctrl via
>>GetKeyState(VK_CONTROL)

That somewhat sounds familar to me - ooops, yes, saw it on this thread:

Question History
Comment
From: jkr  Date: 06/25/2001 11:22AM PST  
You can check the state of any key at any time using the 'GetKeyState()' function.  
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Suggested Solutions

zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

739 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