Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

processing key combinations

Posted on 2001-06-25
10
Medium Priority
?
263 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
  • 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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 

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 450 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying 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

This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

877 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