2

I'm trying to detect the Print Screen key on my form, but keys like Prtsc and SysRq don't fire the KeyDown event..

    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        // Trying to detect if it fires KeyDown, but it doesn't
        MessageBox.Show(e.KeyValue.ToString());
    }

I can't figure it out, maybe I'm really dumb..

Humayun Shabbir
  • 2,961
  • 4
  • 20
  • 33
Joery
  • 759
  • 4
  • 13
  • 33
  • possible duplicate of [How do i capture the Print Screen key?](http://stackoverflow.com/questions/1191479/how-do-i-capture-the-print-screen-key) – user287107 Jul 26 '14 at 20:47

2 Answers2

2

It can be done, but it's not straightforward. You can't do it with the KeyPress or KeyDown events: as you have discovered, it doesn't make them fire.

But you can still do it with c#: you just have to use the Windows APIs. Because the relevant code is lengthy, I'm posting the link:

Capturing the Print Screen Key

Incidentally, you're not dumb. :) Even though this seems like it should have an obvious, simple answer, it doesn't: this is genuinely complex to make happen. But it can be done.

Ann L.
  • 13,760
  • 5
  • 35
  • 66
1

The Key handling events will work only when a key is pressed while the form has focus. The Form properties events and methods are described in http://www.tutorialspoint.com/vb.net/vb.net_forms.htm. the following code will display the pressed key name(vb.net) in message box

Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        MsgBox(e.KeyCode.ToString)
End Sub 

You can trace out the Print screen key's press by using the following code:

Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    If e.KeyCode = Keys.PrintScreen Then
        MsgBox("Print screen key is pressed")
    End If
End Sub

Note :- This will not work in laptops in which the PrintScreen is achieved through function key.
For key code reference: http://msdn.microsoft.com/en-in/library/aa243025(v=vs.60).aspx

  • I think their is no problem with your code the reason is that your form doesn't have focus while the key is pressed.

  • The tab index 0is assigned to any other control in the form so the initial focus will set to that control

Sorry am not so good in C#, you can refer http://converter.telerik.com/ for code conversion

Suji
  • 1,326
  • 1
  • 12
  • 27
  • What I'm actually trying to do is creating a `HotKey Control`, so there is focus on the form. – Joery Jul 26 '14 at 21:11
  • 3
    @ joery : It is clearly mentioned that the keyDown event will work only when the form having focus. http://www.tutorialspoint.com/vb.net/vb.net_forms.htm (form events. 10) – Suji Jul 26 '14 at 21:20