locked
How to assign keyboard shortcut like (Alt+O)for "Ok" Button? (Lata) - 11/30/2007 12:30 AM PST RRS feed

  • Question

  • By: Lata.


    I have tried <accesskey>_Ok</accesskey> for "Ok" Button, but the problem with 
    this is, the button gets clicked when user presses 'O'.
    
    I want that the button should get clicked only when the user presses "Alt+O"
    
    Is there any way out for this? 
    
    Thanks&Regards,
    Lata.
    
    Thursday, February 21, 2008 8:22 PM

All replies

  • By: Corrado Cavalli [MVP]

    You can try handling the PreviewKeyDown event, something like:
    
    <Grid PreviewKeyDown="LayoutRoot_PreviewKeyDown" .... />
    
    private void LayoutRoot_PreviewKeyDown (object sender, 
    System.Windows.Input.KeyEventArgs e)
      {
        e.Handled = (e.OriginalSource == button1 && Keyboard.Modifiers != 
    ModifierKeys.Alt);
      }
    
    where button1 is your button...
    
    -- 
    Corrado Cavalli [Microsoft .NET MVP-MCP]
    UGIdotNET - http://www.ugidotnet.org
    Weblog: http://blogs.ugidotnet.org/corrado/
    "Lata." <Lata.@discussions.microsoft.com> wrote in message 
    news:5B1B3D6D-BEAF-4E1D-ABC3-2EE79D78AFEC@microsoft.com...
    >I have tried <accesskey>_Ok</accesskey> for "Ok" Button, but the problem 
    >with
    > this is, the button gets clicked when user presses 'O'.
    >
    > I want that the button should get clicked only when the user presses 
    > "Alt+O"
    >
    > Is there any way out for this?
    >
    > Thanks&Regards,
    > Lata.
    > 
    
    
    Friday, February 22, 2008 9:43 PM
  • By: Horst Klein

    Hi Lata.
    
    Have you ever tryed it with an underscore "_" like this:
    <Button x:Name="ButtonCancel" Content="_Cancel" Click="OnMyClick">
    
    Best regards 
    Horst
    Friday, February 22, 2008 9:44 PM