Menu Shortcuts X Windows Shortcuts

  • Thread starter Thread starter Michelle
  • Start date Start date
M

Michelle

Hi

Lets say I have a text in a textbox control and I want to copy it.
Plus, I have a menuitem with a shortcut defined as Ctrl+C. Each menuitem
runs a script.
So I hit Ctrl + C in the textbox and BOOM, the menuitem executes first and
no text is copied.

I'm not sure of what I want, cause sometimes I will want the menu to "run",
and sometimes I need Windows to process the shorcut. It depends on what
control is foccused AND if the shorcut is handled by Windows (Ctrl + C, Ctrl
+ V)

I don't see how any key event can help me on this one.
Any ideas?

Thanks
 
I wouldn't do this if I was you. It's only going to confuse users -
personally I'd be scared to hit CTRL-C because how could I be sure that a
script wasn't going to run when I just wanted to copy some text, I hit
CTRL-C instinctively too, I'd accidentally run the script all the time.

Come up with a different functional solution (i.e. don't use CTRL-C to do
whatever is going on in the MenuItem.Click event).

If you insist on heading down this path, you could alter the code that is
executed on the MenuItem.Click event to determine if the context requires a
TextBox copy operation or a script launch. If you need to run the script run
the code you currently have, if you need to provide the Copy functionality
that would have been provided by the TextBox by default call textBox.Copy()

John.
 
Thanks for sharing your thoughts.
I'll try to change the scripts for now, and If I go nuts I'll just remove
the menu shortcut.
I can't just assign another shortcut to the Copy menuitem cause it HAS
indeed a copy functionality, that would confuse the users even more. :S

Michelle
 
Hi Michelle,

If you code something like this, then if the input focus is on any type of
text box then CTRL-C will copy the highlighted text to the clip board, in
any other case it can run your script.

private void menuItem2_Click(object sender, System.EventArgs e) {
Control focused = this.FindFocusedControl();
if (focused is TextBoxBase) {
((TextBoxBase)focused).Copy();
}
else {
// run complex copy
}
}

private Control FindFocusedControl() {
foreach (Control child in this.Controls) {
if (child.ContainsFocus) return this.FindFocusedControl(child);
}
return null;
}

private Control FindFocusedControl(Control control) {
Debug.Assert(control.ContainsFocus);
if (control.Focused) return control;
foreach (Control child in control.Controls) {
if (child.ContainsFocus) return this.FindFocusedControl(child);
}
return null;
}

There is probably a much better way to find the control that has the input
focus, but it's 4am and I don't want to think about it.. :P

John.
 
Back
Top