B
B
Betsq92020-11-03 14:26:33
WPF
Betsq9, 2020-11-03 14:26:33

How to implement key selection in a program so that a certain event is triggered by pressing the key that I have selected?

I need to implement the selection of keys in the program, so that by pressing the key that I have selected, a certain event is called.
There is this code:

gkh.HookedKeys.Add(Keys.F10); 
gkh.KeyUp += new KeyEventHandler(button2_Click);

I need, where "Keys.F10" is, to transfer instead of F10 the key that the user has chosen in the program.
I want to create a form which will have a selection of keys. It doesn't matter if it's a drop-down list or a form like in games in the control settings. I've searched everywhere but I can't find how to do it anywhere.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
Johnny Gat, 2020-11-05
@Betsq9

For WPF, for example:

spoiler

MainWindow.xaml
<Window x:Class="ApplicationWPF.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ApplicationWPF"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid PreviewKeyUp="Grid_PreviewKeyUp">
        <Label Content="Command1" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top"/>
        <ComboBox Name="comboBox1" HorizontalAlignment="Left" Margin="87,10,0,0" VerticalAlignment="Top" Width="120"/>
        <Button Content="Применить" HorizontalAlignment="Left" Margin="212,10,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
        <TextBox Name="textBox1" HorizontalAlignment="Left" Height="254" Margin="10,56,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="277"/>
    </Grid>
</Window>

MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Input;

namespace ApplicationWPF
{
    public partial class MainWindow : Window
    {
        // https://docs.microsoft.com/ru-ru/dotnet/api/system.windows.forms.keys
        private Dictionary<string, Key> keys = new Dictionary<string, Key>
        {
            { "F1", Key.F1 },
            { "F2", Key.F2 }
        };

        private Key keyCommand1;

        public MainWindow()
        {
            InitializeComponent();

            foreach (KeyValuePair<string, Key> key in keys)
            {
                comboBox1.Items.Add(key.Key);
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            keyCommand1 = keys[comboBox1.SelectedItem.ToString()];
        }

        private void Grid_PreviewKeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == keyCommand1)
            {
                textBox1.Text += "Нажата клавиша " + e.Key.ToString() + " - Выполнение команды 1" + Environment.NewLine;
            }
        }
    }
}

For WF, for example:
spoiler

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace ApplicationWF
{
    public partial class Form1 : Form
    {
        // https://docs.microsoft.com/ru-ru/dotnet/api/system.windows.forms.keys
        private Dictionary<string, Keys> keys = new Dictionary<string, Keys>
        {
            { "F1", Keys.F1 },
            { "F2", Keys.F2 }
        };

        private Keys keyCommand1;

        public Form1()
        {
            InitializeComponent();

            KeyPreview = true;

            foreach (KeyValuePair<string, Keys> key in keys)
            {
                comboBox1.Items.Add(key.Key);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            keyCommand1 = keys[comboBox1.SelectedItem.ToString()];
        }

        private void Form1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == keyCommand1)
            {
                textBox1.Text += "Нажата клавиша " + e.KeyCode.ToString() + " - Выполнение команды 1" + Environment.NewLine;
            }
        }
    }
}

A
Alexander Ananiev, 2020-11-03
@SaNNy32

I think you need something like this https://www.codeproject.com/Articles/442285/Global...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question