0

I want to use buttons to change values of speed and targets when I press arrow buttons but I am not sure how.

I tried to use key down events but it did not work. In fact, when I use arrow buttons they just highlight the buttons and the event does not happen whatsoever.

private void ShootingGallery_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Up)
            {
                // Changing variables
            }
        }

I expect to use arrows as extra way of changing the values of variables of speed and targets

Photo

Sir Rufo
  • 17,561
  • 2
  • 36
  • 68
  • I forgot to mention that my windows application consists of a couple of parts. It has the main form and additional controls where other games are going to be placed. I also did not tell about the key preview property which can be found only in the form_1 but not in shooting gallery. Nevertheless, I tried to use the same event with this property set to true but it did not work either. Photo: https://i.imgur.com/Ywa0ngD.png (Form_1) https://i.imgur.com/mrHM5Bb.png (Running application) – Pashok Kalashnikov Apr 13 '19 at 03:17
  • 2
    have you got the right control selected for your key down event? – traveler3468 Apr 13 '19 at 03:42
  • Maybe you have forgotten to assign that. – Maifee Ul Asad Apr 13 '19 at 03:49
  • Heya, could you please provide more info regarding what's being used to develop this? Is it a WPF, windows Forms, or UWP application? Are you using a game engine like Unity to produce this app? – SharpNip Apr 13 '19 at 03:50
  • It is possible to keydown event happens in other controls. – isaeid Apr 13 '19 at 04:22
  • @SharpNip it's Windows Form App, I used a lot of stuff from this video (https://youtu.be/nLfzH4xOVqo) just for the design part. – Pashok Kalashnikov Apr 13 '19 at 05:12
  • The KeyDown event is raised on the control that has the focus. So the buttons or combobox in this UI design, never the form. In addition the arrow keys are intercepted, used to navigate between controls as you found out. Override the [form's ProcessCmdKey method](https://stackoverflow.com/a/400325/17034) to intercept the arrow keys. – Hans Passant Apr 13 '19 at 05:22
  • @AndrewE regardless of the control either main or shooting gallery part where it's need to be situated, both keydown events do not work for some reason. I've checked the similar program with just one control and buttons work perfectly there. I suspect that the problem is with buttons, because they are highlighted when I press up arrow. https://youtu.be/8y_D5ysqcNk – Pashok Kalashnikov Apr 13 '19 at 05:25
  • I accidentally deleted one's comment but he had a solution with a link to this page. Sorry for that. I will leave the link here.(https://stackoverflow.com/questions/400113/best-way-to-implement-keyboard-shortcuts-in-a-windows-forms-application/400325#400325) – Pashok Kalashnikov Apr 13 '19 at 05:33

0 Answers0