How to capture keystrokes before the foreground window gets it? - c#

I'm doing an application like Google Transliteration (Google input tools) tool in C#. It displays the suggestion window on the top of whatever window we are typing (finds caret location & display just window below it). But the focus (foreground process) is still on the application in which we type (for eg. Ms Word, Notepad etc.).
I want to implement these features of Google Transliteration:
It captures the keystrokes before another process gets it.
While renaming a folder or file, we can select words from the suggestions
window show up by using up & down arrow keys and press enter key. But the
rename text field will be still active. The keystrokes will be
captured before it reaches rename text field.
I want to implement a universal text suggestion list (window) like that of Google Transliteration.
How to capture keystrokes before another process gets it (block foreground process from getting it)? (Main Question)
How to capture keystrokes (up & down arrow keys and enter key) and select an option from the words list without focusing on the 'words list' window?
(Caret should remain active and blinking on the foreground process text field. eg. Folder rename text field)
Someone please help me to solve this problem.
I already found this one, but it not helped me to solve my problem.
Capturing keystrokes without focus

You mix two different problems. The first problem - to catch keyboard input - nothing to do with the second - to make an unfocusable window.
The solution to the first problem is very simple - as stated in your link, there is no other way to do it other than Low-Level windows hooks.
The second problem is more difficult, the solution may require a little experimentation, creativity and knowledge of WinAPI. Examples are here, here and here.

Related

Draw rectangular selection and get corresponding image in WPF c#

I need to write a text recognition (from image) application. The main idea is that while my application is running, I may have the need to transform some text of an image into manageble text. So, in this case with the mouse I need to draw a square around the area that i need to capture and the software must convert the content of the extracted picture into text.
I solved the problem of image recognition. I also find a very easy way to capture from screen.
What I need to do now is be able to select with the mouse the interesting area that is over an other running opplication (for example over a webpage or over an image opened in Paint). That must be like the screencapture on windows7, you create a selection of the screen and this is saved like a picture.
By looking around, I didn't find anything and I don't knwo where to start.
Many thanks
You can achieve that using either a 'tricky' easy way or a real but difficult approach.
The tricky way
Screen Recorder applications usually use this approach:
Whenever user wants to select an area, you display a full screen border less Window with 0% opacity, then user attempts to select the screen area, and he is actually selecting your Window area, so you can receive mouse events and display/draw a rectangular shape to show the selection area to the user.
In this approach, the program needs to know when to display the Window and when to hide it. This can be done by for example defining Hotkeys for capturing:
Program registers a hot key using RegisterHotKey to Windows.
User presses and holds that hot key
Program displays the tricky Window
User selects the interested area, program receives that area using mouse events of the tricky Window
User released the hot key and program hides the Tricky window.
The real way
Using this way, you need to set a message hook in order to receive mouse and keyboard events while user is interacting with desktop not your program. This is not an easy to accomplish approach and I recommend you the first one.

VS 2013 Quick Find loses focus when switching to my application - how do I preserve/restore it programmatically?

I have whipped up a C# clipboard application that stores multiple 'clippings' for later use. I use low-level keyboard hooks to pop open my application's window(s) on command. When the window is closed (or a clipping is double-clicked), it is supposed to paste the selected clipping into the last active window (the window prior to my application's window). I use low-level WINAPI methods to determine the last active application, snag its handle, and then return focus to it before simulating a Ctrl+V keystroke to paste.
This typically works except in one very unique scenario: I am in a WPF application project, Quick Finding in a XAML file, the cursor automatically switches to the body text, not the Quick Find textbox, and pastes it there. It seems to have something to do with the loss of focus/activation, as it moves the cursor whenever I activate another window, regardless of my own application's running.
VB files, C# files, what have you, and XAML opened in WinForm projects do not steal the Quick Find focus when switching between the VS2013 application and my own; upon returning to the last active application, the text pastes into the Quick Find box.
Only the XAML in WPF application projects gives me this problem.
So far. I know it is a fringe case, but I expect to run into more. This program is meant to be used in a coding environment and it's pretty important that it be able to handle these kinds of scenarios.
I've tried getting the internal control handle using code from http://www.codeproject.com/Articles/34752/Control-in-Focus-in-Other-Processes, so that I can return the focus to it, but it seems that the handle for the body text and the handle for the Quick Find text box are the same.
A partial solution is found in: How do I prevent the original form from losing focus when I show another form? The popup window I use is navigated primarily through my low-level shortcuts, and therefore has no need of explicit activation.
Using the mouse on it or any of my other windows (as I expect my users will sometime), will cause it to gain activation and circumvent this fix. However, it's such a fringe case it doesn't seem to matter. Hopefully this helps anyone in a similar situation (if not necessarily specifically this one).

C# Custom paste like feature

I am trying to create a way or inserting text anywhere on the system in Windows, much like the clipboard works. For example, pressing ctrl+shift+1 would 'paste' text set in my application into where the user is currently pointing the cursor.
For example, if my application sets it to be 'local.network' and the user presses ctrl+shift+1 in their browser address bar, it would paste in 'local.network'. I then want to have different values set, depending on what number the user presses. Is this possible or can I not create a system wide function like this?
What you're looking for is how to register a hotkey.
Register more than one hotkey with RegisterHotKey
Set global hotkeys using C#
in response to your followup comment, sorry, I though the question was more about the capturing the key press event. If you need to paste text to a window that you don't own, then this question would be a near duplicate. That question uses WM_SETTEXT but you can easily use WM_PASTE instead.

How to do simple game console in OpenGL using C#?

I need to do simple game console - like from Quake, Counter-Strike or other games for simple 3D game using openGL and C#.
Just press button for example ~ and it will show the console.
I can write my commands in it and it will have effect on my application.
For example it will change value of some variable or call some method from my application.
Console should be in left top corner of the window. It's just window application(no full screen).
Have somebody any idea how to do it? Piece of code - how to draw the the console or some tutorial?
Thanks a lot.
Text Editing is difficult in Open GL. It isn't something you want to start off with. C# has text fields and rich text windows. The simplest way would be to create a Window with a TextField inside it in C#. When the user pressed ~ slide the window in and give it focus. When the user presses enter any text in the field is captured, the field is cleared, and the captured text is sent off to the game to be processed.
To give the full console feel you want to make re-print that entered text back out in. You can do that with another, non-editable text field above the first on. Just concat the recently entered string on the end of text already entered in that field.
I would not recommend starting with this. Get something on the screen, get it responding to the keyboard directly. Then worry about how you are going to have you debug data entered. For now you can just hardcode debug commands to random keys that you don't user for your control scheme.

Desktop Development : Remember Target After Losing Focus

Is it possible to take focus from another program to respond to an event, but then once the response has been handled, switch back to the previous program and restore the cursor where it was?
This sounds like a weird question, but think of it along the lines of
User is typing in a text box in Program A.
A window for Program B pops up and user presses a few buttons to confirm something.
Program B returns control to Program A and restores the selection of the textbox.
If there is a complete C# solution, then great, but this sounds like it might require a little more effort than usual.
Any thoughts?
Personally, doing this could very likely result in the user selecting undesired values in the popup window. For example, someone is typing away in Word. Your application pops up a window, where hitting "Enter" selects a value ... such as the default button. Without wanting to, the user "selected" an incorrect value on your form.
Windows itself tends to do this, and it's very annoying. Quickly double-clicking a desktop shortcut to open an application and then switching back to (for example) an e-mail before the app launches, will tend to remove focus from the focused e-mail window and put focus in the just-opened application, causing your e-mail text or keyboard strokes to go to the just-opened window.
In my experience windows programs work just this way. It often appears that they don't because the user is returning focus with a mouse-click, which itself resets the focus. If it's a winforms app you can probably do something with the lost focus/got focus events at the form level.

Categories

Resources