2

Every windows hook is set to a particular window, or is global. And if I'm not wrong, even the textbox is a window. So, is it possible to set a low level keyboard hook to a specific textbox?

My goal is to capture keydown event on my textbox, but I figured out that using basic method I'm not able to capture the PrintScreen key, so I'm trying to do it another way.

Thanks

Kolan
  • 103
  • 1
  • 5
  • And what if I use WinForms? – Kolan Aug 21 '15 at 13:56
  • are you talking about a compiled program or in your own application code? – Thorarins Aug 21 '15 at 14:01
  • 1
    Almost, but not quite, a duplicate of: http://stackoverflow.com/questions/4292925/keydown-event-not-working-on-printscreen-key – goobering Aug 21 '15 at 14:02
  • @Thorarins It is my own application with my own code. – Kolan Aug 21 '15 at 14:04
  • @goobering I've already read that question and answer. I need to capture the key on KeyDown event. – Kolan Aug 21 '15 at 14:05
  • 1
    A keyboard hook is not associated with a particular window handle. The keyboard hook event fires prior to routing of the pressed key data to the control that currently has focus. – 500 - Internal Server Error Aug 21 '15 at 14:07
  • So I shall set that hook to parent window and in the event handler check if my textbox is currently focused? – Kolan Aug 21 '15 at 14:12
  • There is no problem to [use hook in wpf](http://stackoverflow.com/q/1639331/1997232). What you doing in hook function is up to you of course. My previous comments (deleted) are invalid, because keyboard hook doesn't need window handle (thanks @500-InternalServerError for pointing that). – Sinatr Aug 21 '15 at 14:26

1 Answers1

1

PrintScreen is a key that triggers a system function, e.g. Copy screen to clipboard. The key needs to work no matter what UI control has the keyboard focus and is getting the rest of the keystrokes, e.g. your text box. The way to capture this key is with a keyboard hook. See this answer. I believe the code works in both Winforms and WPF.

David Ching
  • 1,903
  • 17
  • 21