How to raise a shortcut

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I want to have a Button.Click event 'raise' a shortcut.
If eg Button1 is Clicked, I want to have a shortcut 'CTRL + F12' activated.

What's the best way to acchieve this.

TIA,

Michael
 
Michael Maes said:
I want to have a Button.Click event 'raise' a shortcut.
If eg Button1 is Clicked, I want to have a shortcut 'CTRL + F12'
activated.

Call the menu item's 'PerformClick' method.
 
The PerformClick-Method Raises the Click-Event.
What I need is to raise/activate a Shortcut in code (whatever the control
calling the code is).

To make it clear: I could want to use Label.MouseEnter to activate/raise a
certain Shortcut.
 
Michael,

Is it not more normal to perform the code that should be done with the
shortcut?

Cor
 
Hi Michael,

Based on my understanding, there is some logic in your winform application
that intercepts the Ctrl+F12 key combination shortcut. Now, you want to
activate this shortcut code logic in various places, such as Button.Click
event and Label.MouseEnter event etc... If I have misunderstood you, please
feel free to tell me, thanks.

Normally, in this situation, the recommended solution is placing all the
code that is processing the Ctrl+F12 shortcut in a subroutine function, so
we can call this subroutine function in various places to achieve/activate
the same Ctrl+F12 code logic.

If your application has the limitation that the code that processes Ctrl+F12
spreads in many places and is hard to maintain in a single subroutine
function, you may really need to simulate the Ctrl+F12 key combination
programmatically. .Net provided System.Windows.Forms.SendKeys class for this
function. You may use SendKeys.Send() method to simulate the shortcut. More
specificly, you may use SendKeys.Send("^{F12}"). The detailed keystroke
parameter table is listed in the MSDN link below:
http://msdn2.microsoft.com/en-us/library/system.windows.forms.sendkeys.send.aspx

SendKeys.Send() is also useful to trigger keyboard shortcut of other
applications, since other application's code is not callable from our code.

Hope this helps.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notifications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support Engineer
within 1 business day is acceptable. Please note that each follow up
response may take approximately 2 business days as the support professional
working with you may need further investigation to reach the most efficient
resolution. The offering is not appropriate for situations that require
urgent, real-time or phone-based interactions or complex project analysis
and dump analysis issues. Issues of this nature are best handled working
with a dedicated Microsoft Support Engineer by contacting Microsoft Customer
Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Michael,
In addition to the other comments.

Where is the short-cut? (what control has 'CTRL + F12' as a short cut?)

Do you know or is it "arbitrary", by "arbitrary" I mean the user can set it.

If you know that the WhateverMenuItem has 'CTRL + F12' as short cut you can
simply PerformClick on that menu item. As PerformClick is a method available
on Button (as Herfried suggested), MenuItem (.NET 1.x menus), ToolStripItem
(.NET 2.0 menus), and RadioButtons. FWIW: PerformClick is part of the
IButtonControl interface allowing "commands" to expose invoking their
"click" event.

Public Sub WhateverButton_Click(...)
WhateverMenuItem.PerformClick()
End Sub

If its "arbitrary" in that your button has a "short cut" property on it on
what short cut to perform, I would consider using the SendKeys.Send method
to send the "short cut" to the main form in your app & let it then find the
respective short cut...

Public Sub WhateverButton_Click(...)
SendKeys.Send("^{F12}")
End Sub
 
Hi Jeffrey,

Thanks for your reply (as to Cor and Herfried).
You did actually understand the requirements fine.
I need this functionality to call a function of an external application
(Acti).
So it is indeed the reverse of the usual.

I simply didn't consider 'SendKeys'.
Normally I don't exactly like to use SendKeys (because of App-Focus-Issues),
but in this case it could be a perfect solution since it will be invoked by a
button-click (so app-focus is not an issue).

Thanks!

Michael
 
Hi Jay,

Thanks for your input.
It is in fact to invoke a shortcut on our subclassed TextBox (with inner
button).
So the SendKeys is indeed the 'perfect' solution.

Thanks!

Michael
 
Jeffrey,

Nice answer, however "Our code". In my idea are you now busy as a privat
regular in this newsgroup. Even if you where in Hawai it would be at the
moment you wrote it monday 5AM.

:-)

Cor
 
Hi Michael,

Thanks for the feedback.

Yes, normally, the focus issue is the main concern of using 'SendKeys'
approach, and it is easy to be broken by too high frequency UI operations.
Sometime we even need to attach the raw input thread to get this solution
work.

Anyway, glad to see my reply makes sense to you. If you need further help,
please feel free to post. Thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top