Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8658765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:50:50+00:00 2026-06-12T15:50:50+00:00

I have keyboard shortcuts declared in my xaml using KeyBindings. I would like to

  • 0

I have keyboard shortcuts declared in my xaml using KeyBindings.
I would like to ignore repetitions due to key holding in few of them.

I have found only solutions using events and checking “IsRepetition”, which doesnt really fit in my declaration of the keybindings.

Of course I could do it in the Command definition itself and measure a time difference between 2 last executes, but this gives me no way to differentiate multiple presses and 1 key holding.

What would be the best way to execute only on the first press and ignore the rest if the key is hold?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T15:50:51+00:00Added an answer on June 12, 2026 at 3:50 pm

    You are trying to change a behavior of the button. Better to use code for that.
    The easiest way is to attach a preview event to the window like that:

    <Window 
           ...
              PreviewKeyDown="HandlePreviewKeyDown">
    

    Then in code handle it like that:

        private void HandlePreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.IsRepeat)
            {
                e.Handled = true;
            }
        }
    

    Sadly this would disable any repeat behavior, even in a textbox hosted by the form. This is an interesting question. If I find a more elegant way of doing this, I will add to the answer.

    EDIT:

    OK there are two ways to define Key Binding.

        <Window x:Class="WpfApplication1.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525">
    
    
        <Window.InputBindings>
            <KeyBinding x:Name="altD" Gesture="Alt+D" Command="{Binding ClickCommand}"/>
        </Window.InputBindings>
    
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition/>
            </Grid.RowDefinitions>
                <Button Content="_Click" Command="{Binding ClickCommand}" />
            <TextBox Grid.Row="1"/>
        </Grid>
    </Window>
    

    The above button will generate a click because you implicitely requested the Alt-C gesture via the underscore: _Click content. Then the window has an explicit keybinding to Alt+D.

    This code behind should now work for both cases and should not interfere with regular repeat:

    protected override void OnPreviewKeyDown(KeyEventArgs e)
        {
            base.OnPreviewKeyDown(e);
    
            if (e.IsRepeat)
            {
                if (((KeyGesture)altD.Gesture).Matches(this, e))
                {
                    e.Handled = true;
                }
                else if (e.Key == Key.System)
                {
                    string sysKey = e.SystemKey.ToString();
                    //We only care about a single character here: _{character}
                    if (sysKey.Length == 1 && AccessKeyManager.IsKeyRegistered(null, sysKey))
                    {
                        e.Handled = true;
                    }
                }
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to have keyboard shortcuts to Team->Add and Team->Commit in eclipse. I
I would like to have the keyboard already displaying when I show my view.
I'm using GWT and GXT and want to use keyboard shortcuts like CTRL-S (or
I realize that iOS has the ability to have keyboard shortcuts (e.g.: Command-C for
I have this textbox I use to capture keyboard shortcuts for a preferences config.
What specific techniques have you found to learn keyboard shortcuts while programming? I don't
Is it possible to have keyboard event listener canvas.addEventListener('onkeydown', ev_keydown, false); like we have
I have a lot of custom keyboard shortcuts set up. To avoid having to
I'd like to create keyboard shortcuts for some controls in my Windows Forms application.
Using a macbook keyboard (which means I do not have the 'page up' button)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.