tags:

views:

35

answers:

1

I have a text box on a form that is incremented each time an alt and print screen key are pressed, and specifically on the up of the print screen key. This all works fine. But what I need to accomplish is to keep that happening even when the focus has changed. Below is my code so far: I have been told to create a hook for this as it's outside of my application so any help on this would be greatly appreciated...Thanks

public Form2()
{
    InitializeComponent();
    this.KeyUp += new KeyEventHandler(Form2_KeyUp);
}

public void Form2_KeyUp(object sender, KeyEventArgs e)
{
    //If the Alt key was pressed and the PrintScreen key was the one released, bump                        
    //the counter and update the textbox.

    if (e.Alt && (e.KeyCode == Keys.PrintScreen))
        count++;
        try
        {
            if (count == 0)
                textBox1.Text = "0";
            else
            {
                textBox1.Text = ("" + count);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
        finally
        {

        }
    }
}
+2  A: 

Take a look at this article. It tells you how to register a Windows hook, and as an example, it specifically mentions keyboard events.

Jacob
let me if I may..explain what I am attempting to accomplish..I have a form with 3 buttons and 1 Text Box..when the user presses the Alt and print screen keys it increments the Text box as a number each time..This works fine. However, when out of focus it does not increment the Text Box. So I have tried the link you suggested and no luck..but I do appreciate your link..if there is another way or suggestions it would be great..
Jim