Dialog Handling Shortcut keys

E

Emma Middlebrook

Hi there,

I'm trying to handle some shortcut keys within my application and I
can't seem to get the code to work when you are trying to action
against a ctrl + other character.

I found a post
http://groups.google.co.uk/group/mi...ortcut+key+ctrl&rnum=3&hl=en#27cbc8062c107680
that appears to help a lot but it doesn't seem to work with the ctrl
key.

here's the snippet of code I am working with, which was modified from
the thread mentioned above:

protected override bool ProcessDialogKey(Keys keyData)
{
if (keyData == Keys.ControlKey && keyData == Keys.G)
{
ReassignAssessment();
}
else if (keyData == Keys.Insert)
{
ToggleReadUnread();
}

return base.ProcessDialogKey(keyData);
}

This method seems to be fired on every key press, so I don't even get
a chance to press "G" as the method has already fired on ctrl. Even
when I run without debugging it's showing no signs of executing.

The value of keydata when I do debug is 131089, which if I look at the
MSDN help could match to the value of Keys.Control (131072) +
Keys.ControlKey (17)

Are there any alternatives that I could try to get this to work?

Thanks,

Emma Middlebrook
 
J

Jon Skeet [C# MVP]

This method seems to be fired on every key press, so I don't even get
a chance to press "G" as the method has already fired on ctrl. Even
when I run without debugging it's showing no signs of executing.

You need to use:

if (keyData == (Keys.G | Keys.Control))
{
....
}

This will trigger if G and Ctrl are pressed, but won't trigger on:
Shift+G
G
Shift
Ctrl
Ctrl+Shift+G
Alt+G
etc

Jon
 

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