Capture Keypress in WPF C# Application when not in focus - c#

I want to capture the PrintScreen button even if the application is minimised or not on focus at all. I've seen some snippets but none of these worked in WPF, only in Winforms. Anyone can give some code to start?

Please refer to the following blog post for an example of how you could implement global hot keys in a WPF application.
Implementing global hot keys in WPF: https://blog.magnusmontin.net/2015/03/31/implementing-global-hot-keys-in-wpf/.
A global, or system-wide, hot key is a key or a combination of keys that is associated with a particular window, which will receive messages whenever the user presses this key or key combination from anywhere in the system.
There are indeed no .NET API:s to register hot keys for your application but there is a native RegisterHotKey method that you can call from managed code using P/Invoke. Please refer to the link for more information.

What you want is called "Global Keyboard Hooks"
there is a pretty good solution here: http://www.codeproject.com/KB/cs/globalhook.aspx

Related

How can I capture a key press, outside of the form?

I have been trying to capture the keys pressed outside of my winform, but obviously a KeyPress event won't work.
I haven't been able to get any closer than the KeyPress event, which only works on the form level, as specified
I suspect that I will have to do the
[DllImportAttribute("user32.dll")]
, but I have little to no experience with that.
Being able to capture key presses anywhere requires using Hooks.
There is a library on CodePlex which simplifies implementing Application and Global Mouse and Keyboard Hooks for C# users.

Global Hotkeys - C#

I have built an application which listens for a global hot key (Alt + Space currently), when I press the hot key when Google Chrome is in focus it brings my app to focus but also opens Chrome's context menu (right click menu). I used the following dll for implementing the keyboard hook.
Can I prevent this from happening? If so how? This isn't a major problem but if there is a way in which you can clear all existing hot keys tied to that combination then I'd like to learn this.
EDIT
The answer that I selected as being the correct one, is the correct one but for my problem. To correctly implement this feature into a C# WinForm i used this tutorial which was very helpful: http://www.dreamincode.net/forums/topic/180436-global-hotkeys/
The FAQ section of the linked article contains your answer:
Question
I need to suppress some keystrokes after I have processed them.
Answer
Just set the e.Handled property to true in the key events you have
processed. It prevents the keystrokes being processed by other
applications.

Creating a Process in the background that will listen to keyboard

I have a project that runs in the background in a different process, I want it to be able to react to keyboard everywhere, for example I run the project, and afterwards I do other stuff in the computer such as browsing, facebook, watching movies etc.., and every time I press F9 I want my project to show up. Same as how you press a combination of keys to invoke Babylon... I want to implement it in C#, I have no idea how to begin.
You can register a hotkey with the RegisterHotKey API function. You can see an example of its usage from C# here.
I think you need to write a system-wide keyboard hook, check here for details:
https://stackoverflow.com/a/1764434/559144
How do I grab events for all applications? An example system-wide hook.

Cancel Key Stroke after GetAsyncKeyState match?

I'm creating a utility for my mother-in-law in order to remap CTRL-C and CTRL-V commands to one signal button on her keyboard to assist with at home work. On the first press it will commit a COPY command. Next press will be a PASTE command. Since my little application won't be in focus I'll need the use of GetAsyncKeyState. Right now I'm attempting to map this all to the tilde key. How do I kill the tilde key from actually passing to the field selected and overwriting the data or appending itself to the pasted text? Could either use C# in any .NET framework or VB6 for this little applet.
I understand the answer I'm looking for could assist in creating malicious software so if anyone should feel the need to not share or delete this question then by all means do so. I'm looking for an answer to a real issue or situation. I know I could map to a none typing key like Scroll Lock or Caps Lock like some other utilities out there but both of these keys are used at this point in time and not quite an option.
You going to have to do a global keyboard hook (see VBAccelerator for some details on global hooks in VB6), and then not pass on the message once you have received CTRL-V or CTRL-C and then act on that.

Send keys to WPF Browser control

Can I programatically send [UserID]{TAB}[Password]{CARRIAGE RETURN} to a webbrowser control which has a userID, password and Sign-in button there. I wanted to use my own virtual keyboard in my application. Any tips here?
Sorry for the late answer but I've just finished a similar project and as part of the work am in the process of open sourcing two projects to Codeplex.
The first is the Windows Input Simulator which is a simple .NET wrapper around the Win32 SendInput written in C#.
The second is a very customisable on screen keyboard or touch screen keyboard control and toolkit called WpfKB and will be available as an initial release tomorrow. Hope these are of help to you or anyone else who comes across the projects.
I recently had to implement automatic authentication through a WPF browser control, and I looked into simulating keystrokes. I didn't need a full virtual keyboard so interacting with the DOM of the login page through IHTMLDocument2 ended up being the best approach, but I looked into keystroke automation before making that decision and found a few options.
You can raise the appropriate routed events on the control as described in Simulating basic keyboard events and Simulating text input. I don't know of any specific problems with this approach but I opted against it simply because I wasn't comfortable simulating input without looking at how the CLR handles the actual input, and without at least raising the complete lifetime (PreviewKeyDown, KeyDown, PreviewKeyUp, KeyUp) I was wary of unintended consequences.
Take a look at WOSK on CodePlex. It's a good example of how to invoke Win32 keybd_event and SendInput functions to generate the low-level input messages via Managed Windows API to simulate input. There's some unnecessary fluff (eg transparency) and some odd WPF usage, such as using a CommandParameter with a Click event instead of a Command on the buttons, but the general approach is sane and it's reasonably complete.
You can also invoke the windows on-screen keyboard as alluded to by Jeroen. I didn't try this because I didn't need a virtual keyboard, but if you're going to call into Win32 anyway, you might as well follow the WOSK model and build the UI the way you want it.

Categories