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

  • SEARCH
  • Home
  • 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 609175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:32:16+00:00 2026-05-13T17:32:16+00:00

I’ve recently started using WPF for developing my applications. Now I’ve come to a

  • 0

I’ve recently started using WPF for developing my applications. Now I’ve come to a point where I need some tips on good design when it comes to key combination handling.

This is what I’m using at the moment:

private void Grid_PreviewKeyDown(object sender, KeyEventArgs e)
{
    if (Keyboard.Modifiers == ModifierKeys.Control)
    {
        switch (e.Key)
        {
            case Key.Up: PreviousLine(); break;
            case Key.Down: NextLine(); break;
            case Key.Return: NextLine(); break;
        }
    }
    else if (Keyboard.Modifiers == ModifierKeys.Shift)
    {
        switch (e.Key)
        {
            case Key.Return: PreviousLine(); break;
        }
    }
}

As you can imagine, this will start to get really ugly, really fast.

Do you have any tips which would improve the code?

  • 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-05-13T17:32:16+00:00Added an answer on May 13, 2026 at 5:32 pm

    IMVHO there is nothing too much wrong with what you are doing, as long as it is confined to the View.

    The only thing to discuss is how to smooth out the testing of key states. How you structure this is largely over to personal preference, everyone will have a slightly different take on it. Although you don’t want endless else if statements, or lots of duplicated switch statements, and you don’t want the handler to be 1000 lines long.

    What about the following:

    private void Grid_PreviewKeyDown(object sender, KeyEventArgs e)
    {
        bool shiftPressed = Keyboard.Modifiers == ModifierKeys.Shift;
        bool ctrlPressed = Keyboard.Modifiers == ModifierKeys.Control;
    
        switch (e.Key)
        {
            case Key.Up:
                e.Handled = ctrlPressed ? PreviousLine() : false; 
                break;
            case Key.Down:
                e.Handled = ctrlPressed ? NextLine() : false; 
                break;
            case Key.Return:
                e.Handled = ctrlPressed ? NextLine() : shiftPressed ? PreviousLine() : false; 
                break;
        }
        e.Handled = false;
    }
    

    I created the shiftPressed and ctrlPressed bools so that i could eliminate the surrounding if statement (and any duplication that goes with it) and use the ternary statement instead. For this to work, you will need to return a bool from your NextLine() and PreviousLine() functions – that might seem stupid but they may not always be able to do what they should, i.e. NextLine() can return false if you are on the bottom row of the grid.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 346k
  • Answers 346k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Well, u.courses is a Collection and doesn't have a students… May 14, 2026 at 6:05 am
  • Editorial Team
    Editorial Team added an answer In your Main() method, you need to capture the file… May 14, 2026 at 6:05 am
  • Editorial Team
    Editorial Team added an answer Red5 is a server which supports video streaming, messaging (through… May 14, 2026 at 6:05 am

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a French site that I want to parse, but am running into
I have text I am displaying in SIlverlight that is coming from a CMS

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.