ProcessCmdKey vs ProcessDialogKey

G

Guest

Hi,

I am still confused the different between ProcessCmdKey and
ProcessDialogKey. When to use it? I write a simple user control and these 2
override functions looks the same to me.

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
const int WM_KEYDOWN = 0x100;
const int WM_SYSKEYDOWN = 0x104;

if(msg.Msg == WM_KEYDOWN || msg.Msg == WM_SYSKEYDOWN)
{
switch(keyData)
{
case Keys.Alt | Keys.S:
MessageBox.Show("Alt+S");
break;
// some more cases...
}
}
return true;
}

==============================================

protected override bool ProcessDialogKey(Keys keyData)
{
switch(keyData)
{
case Keys.Alt | Keys.S:
MessageBox.Show("Alt+S");
break;
// some more cases... }
return true;
}

==============================================

Another thing, neither one of them capture "Alt+Tab" (Keys.Alt | Keys.Tab)
combinations. Anyone knows why? Or how to capture "Alt+Tab"?

Please advice, thanks!
-P
 
A

Andy Mortimer [MS]

Hi,
You can determin Ctrl and Alt like this:-

Protected Overrides Function ProcessCmdKey(ByRef msg As
System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys)
As Boolean

System.Diagnostics.Debug.WriteLine(keyData.ToString())
keyData = keyData And keyData.Modifiers
If (keyData = keyData.Control + keyData.Alt) Then
Return True
End If

Return False

End Function

They seem to be clearly documented here
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemwindowsformscontrolclassprocessdialogkeytopic.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemwindowsformscontrolclassprocesscmdkeytopic.asp

I don't think there's much between them, apart from the extra msg info.
Though you should really use them in the way they are intended. For example
the ProcessDialogKey will not receive all key and modifier combinations .
It may be appropriate which you use depending on what control you are
implementing and what you want ot do, for example adjusting up down
behaviour in a datagrid would have to be done in ProcessCmdKey as that will
handle it first and move to next cell,

Andy Mortimer [MS]
Please do not send email directly to this alias. This alias is for
newsgroup purposes only

This posting is provided "AS IS" with no warranties, and confers no rights.
OR if you wish to include a script sample in your post please add "Use of
included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top