Solved

processing key combinations

Posted on 2001-06-25
10
251 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:chandas
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 5

Expert Comment

by:robpitt
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
>>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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This article shows how to make a Windows 7 gadget that accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
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 …
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…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now