-4

I want to do this. There are 4 buttons, like arrow keys in my form. When I pressed the button in left, I want to do it what the Real Left Arrow key do. Like that I want all 4 buttons to do. I searched and got only about the KeyEven function, I'm Self-Learning. Any help will appreciated.

Thanks.

  • What should *the real left arrow key do?* – Yuck Jun 22 '13 at 12:54
  • What it do normally. Like when u do gaming. – user2511652 Jun 22 '13 at 12:58
  • What I mean is, what's the context of the arrow key? Do you want it to move a cursor left or right? Do you want it to shift a panel (or another control) up or down? It's unclear how the arrow keys should affect the application. – Yuck Jun 22 '13 at 13:00
  • He wants an onscreen left arrow button which can be used interchangeably with the physical left arrow button on the keyboard. – Tormod Jun 22 '13 at 13:00
  • Sorry, It's for using it in a game. – user2511652 Jun 22 '13 at 13:01
  • 1
    See http://stackoverflow.com/questions/1645815/how-can-i-programmatically-generate-keypress-events-in-c – unlimit Jun 22 '13 at 13:01
  • Yeah, u right Tormod. Due to my bad English, I can't understand somethings. Forgive me – user2511652 Jun 22 '13 at 13:02
  • @Tormod You're missing the point. Hooking up a button and key press event to the same method is simple. The disconnect here is that it is unclear to me control the key press should be bound to. – Yuck Jun 22 '13 at 13:03
  • Sorry, here it again. Imagine that U have opened a game and it's in windowed mode. And I open My application, so I don't use keyboard, and I do the gaming by using the app. When I wanted to press the Left key, I press the one in my app – user2511652 Jun 22 '13 at 13:06
  • Hi again, Tormods code isn't working for me – user2511652 Jun 22 '13 at 13:10
  • If you have two applications running - (1) a game and (2) the one you're writing - you're going to have to use IPC to send commands to the game from your application. Good luck. – Yuck Jun 22 '13 at 13:11
  • I used this code from him, var key = Key.Escape; // Key to send var target = Keyboard.FocusedElement; // Target element var routedEvent = Keyboard.KeyDownEvent; // Event to send target.RaiseEvent( new KeyEventArgs( Keyboard.PrimaryDevice, PresentationSource.FromVisual(target), 0, key) { RoutedEvent = routedEvent } ); – user2511652 Jun 22 '13 at 13:11
  • Thx for the tip Yuck, I will look for it. – user2511652 Jun 22 '13 at 13:12
  • You'd need to make it so your app can't receive focus with WS_EX_NOACTIVATE, then use SendKeys() to send the arrows when the buttons are pressed. See my answer [here](http://stackoverflow.com/a/17001367/2330053) for a WinForms solution. – Idle_Mind Jun 22 '13 at 13:12
  • Thx, Idle_Mind I'm trying couple of things here, but still couldn't acheive my thing. – user2511652 Jun 22 '13 at 13:24

1 Answers1

0

From an MSDN article:

Some key presses, such as the TAB, RETURN, ESC, and arrow keys, are typically ignored by some controls because they are not considered input key presses. For example, by default, a Button control ignores the arrow keys. Pressing the arrow keys typically causes the focus to move to the previous or next control. The arrow keys are considered navigation keys and pressing these keys typically do not raise the KeyDown event for a Button. However, pressing the arrow keys for a Button does raise the PreviewKeyDown event. By handling the PreviewKeyDown event for a Button and setting the IsInputKey property to true, you can raise the KeyDown event when the arrow keys are pressed. However, if you handle the arrow keys, the focus will no longer move to the previous or next control.

I am quoting the code I found there. I have not tested it

public Form1()
{
  InitializeComponent();

  // Form that has a button on it
  button1.PreviewKeyDown +=new PreviewKeyDownEventHandler(button1_PreviewKeyDown);
  button1.KeyDown += new KeyEventHandler(button1_KeyDown);
  button1.ContextMenuStrip = new ContextMenuStrip();
  // Add items to ContextMenuStrip
  button1.ContextMenuStrip.Items.Add("One");
  button1.ContextMenuStrip.Items.Add("Two");
  button1.ContextMenuStrip.Items.Add("Three");
}

// By default, KeyDown does not fire for the ARROW keys 
void button1_KeyDown(object sender, KeyEventArgs e)
{
  switch (e.KeyCode)
  {
    case Keys.Down:
    case Keys.Up:
        if (button1.ContextMenuStrip != null)
        {
            button1.ContextMenuStrip.Show(button1,
                new Point(0, button1.Height), ToolStripDropDownDirection.BelowRight);
        }
        break;
  }
}

// PreviewKeyDown is where you preview the key. 
// Do not put any logic here, instead use the 
// KeyDown event after setting IsInputKey to true. 
private void button1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
  switch (e.KeyCode)
  {
    case Keys.Down:
    case Keys.Up:
        e.IsInputKey = true;
        break;
 }
}

Here is the link http://msdn.microsoft.com/en-us/library/system.windows.forms.control.previewkeydown.aspx

Hope this helps you, good luck.

unlimit
  • 3,672
  • 2
  • 26
  • 34