Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The program below has two buttons that they show and hide 2 pictures by clicking on them successively...
What i want is to set a particular button on the keyboard, say the A button, and by pressing it to hide both picturs

Can you give me an example of such keyboard command in WPF?

Thanks

What I have tried:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp2
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        { if (linevertical.Visibility == Visibility.Hidden)
            { linevertical.Visibility = Visibility.Visible; }
            else { linevertical.Visibility = Visibility.Hidden; }
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        { if (lineo.Visibility == Visibility.Hidden)
            { lineo.Visibility = Visibility.Visible; }
            else { lineo.Visibility = Visibility.Hidden; }
        }
    }
}
Posted
Updated 10-Apr-19 5:50am

Sounds like you want KeyBinding (aka Hotkey, aka keyboard shortcut)
Have a look at the solutions to this CP post How to Create Short Cut Keys in WPF MVVM pattern[^]
Reference Documentation here [^]
Basically bind a KeyGesture to the button click in your XAML. I would probably combine those two methods into one though
C#
private void Button_Click(object sender, RoutedEventArgs e)
{ 
        HideMyObject(linevertical);
        HideMyObject(lineo);

}
private void HideMyObject(object myObject)
{
        if (myObject.Visibility == Visibility.Hidden)
        { 
                myObject.Visibility = Visibility.Visible; 
        }
        else 
        { 
                myObject.Visibility = Visibility.Hidden; 
        }
}


Edit - OP does not want to use binding. Try this instead
C#
private void Window_KeyDown(object sender, KeyEventArgs e)
{
        if (Keyboard.Modifiers == ModifierKeys.Control && e.Key == Key.A)
        {
                HideMyObject(linevertical);
                HideMyObject(lineo);
        }
}
Note this would do the hiding if Ctrl+A is used. It's a bad idea to just the A-key unless there is absolutely never going to be any keyboard input to the window.
 
Share this answer
 
v2
Comments
tool__ 10-Apr-19 10:46am    
Thanks for your answer, if you click on the main window on the designer and then go to properties tab\ event handlers , there is an option there KeyDown, if you click it, it puts on the code behind this:

private void mainwindow_KeyDown(object sender, KeyEventArgs e)
{

}

somehow you say there in the brackets: IF ''A'' key pressed do that...
but i dont know how to syndax it...
this way i want to do it i don't want to use MVVM and binding, if there is a simpler way to do it, and i dont want to use things i dont understand in general
CHill60 10-Apr-19 12:25pm    
I've update my solution
tool__ 10-Apr-19 13:08pm    
Yes will be absolutely no keyboard inputs in the program. Have a look if you can in the solution i posted below, what's your opinion on that? it works...
CHill60 11-Apr-19 3:43am    
When you say "i can't use simple letters, it gives me errors", specifically what are the errors?
tool__ 11-Apr-19 6:33am    
I fixed it, I was using quotation marks with the letter ''A'' and some other things... that's why underlined it and gave me errors, it needs just the letter and it's fine, Thanks
I found out this which works:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp2
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        { if (linevertical.Visibility == Visibility.Hidden)
            { linevertical.Visibility = Visibility.Visible; }
            else { linevertical.Visibility = Visibility.Hidden; }
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        { if (lineo.Visibility == Visibility.Hidden)
            { lineo.Visibility = Visibility.Visible; }
            else { lineo.Visibility = Visibility.Hidden; }
        }

        private void Mainwindow_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.F5)
            {
                lineo.Visibility = Visibility.Hidden;
                linevertical.Visibility = Visibility.Hidden;
            }
        }
    }
}


But only works for F keys , i can't use simple letters, it gives me errors, any ideas guys? i want to use the ''A'' letter in place of the F5...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900