Home All Groups Group Topic Archive Search About

How to raise a shortcut

Author
15 Oct 2006 9:16 AM
Michael Maes
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

Author
15 Oct 2006 11:21 AM
Herfried K. Wagner [MVP]
"Michael Maes" <michael.maes@community.nospam> schrieb:
> 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.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>
Author
15 Oct 2006 12:30 PM
Michael Maes
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.


Show quoteHide quote
"Herfried K. Wagner [MVP]" wrote:

> "Michael Maes" <michael.maes@community.nospam> schrieb:
> > 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.
>
> --
>  M S   Herfried K. Wagner
> M V P  <URL:http://dotnet.mvps.org/>
>  V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>
>
>
Author
15 Oct 2006 2:55 PM
Cor Ligthert [MVP]
Michael,

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

Cor

Show quoteHide quote
"Michael Maes" <michael.maes@community.nospam> schreef in bericht
news:FD0DABEF-239D-4C6A-86EC-86CC840FA868@microsoft.com...
> 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.
>
>
> "Herfried K. Wagner [MVP]" wrote:
>
>> "Michael Maes" <michael.maes@community.nospam> schrieb:
>> > 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.
>>
>> --
>>  M S   Herfried K. Wagner
>> M V P  <URL:http://dotnet.mvps.org/>
>>  V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>
>>
>>
Author
15 Oct 2006 3:24 PM
Michael Maes
Hi Cor,

The code is "an external application" (not ours) so that's not an option.

Show quoteHide quote
"Cor Ligthert [MVP]" wrote:

> Michael,
>
> Is it not more normal to perform the code that should be done with the
> shortcut?
>
> Cor
>
> "Michael Maes" <michael.maes@community.nospam> schreef in bericht
> news:FD0DABEF-239D-4C6A-86EC-86CC840FA868@microsoft.com...
> > 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.
> >
> >
> > "Herfried K. Wagner [MVP]" wrote:
> >
> >> "Michael Maes" <michael.maes@community.nospam> schrieb:
> >> > 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.
> >>
> >> --
> >>  M S   Herfried K. Wagner
> >> M V P  <URL:http://dotnet.mvps.org/>
> >>  V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>
> >>
> >>
>
>
>
Author
15 Oct 2006 3:00 PM
Jeffrey Tan[MSFT]
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.
Author
15 Oct 2006 3:24 PM
Michael Maes
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

Show quoteHide quote
"Jeffrey Tan[MSFT]" wrote:

> 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.
>
>
>
>
Author
16 Oct 2006 6:23 AM
Jeffrey Tan[MSFT]
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.
Author
15 Oct 2006 4:01 PM
Cor Ligthert [MVP]
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

Show quoteHide quote
"Jeffrey Tan[MSFT]" <je***@online.microsoft.com> schreef in bericht
news:OilmgqG8GHA.3396@TK2MSFTNGP04.phx.gbl...
> 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.
>
>
>
Author
15 Oct 2006 3:14 PM
Jay B. Harlow
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


--
Hope this helps
Jay B. Harlow
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


Show quoteHide quote
"Michael Maes" <michael.maes@community.nospam> wrote in message
news:1718D308-DDB0-4F87-9E7E-CF5DF50C7E06@microsoft.com...
> 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
Author
15 Oct 2006 3:45 PM
Michael Maes
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


Show quoteHide quote
"Jay B. Harlow" wrote:

> 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
>
>
> --
> Hope this helps
> Jay B. Harlow
> .NET Application Architect, Enthusiast, & Evangelist
> T.S. Bradley - http://www.tsbradley.net
>
>
> "Michael Maes" <michael.maes@community.nospam> wrote in message
> news:1718D308-DDB0-4F87-9E7E-CF5DF50C7E06@microsoft.com...
> > 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
>