Writing in notepad with keyboard application - c#

Keyboard simulator ، Like Like On-Screen Keyboard
how to make Like "On-Screen Keyboard " ?

In old win 32 API there was a sendKeys() method like API. when user clicks your form, then your form is active and has focus while he has activated the desired control for writing just before giving focus to yours. One solution is monitoring focus changes on controls and when user presses keyboard on your form, you can sendKeys() to appropriate window handle. I don't know what's the real name of that method and it's equivalent ic .NET but it should not be hard to find out. Stick to design.
[edited]
here it is :
http://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.aspx

Related

C# Disable mouse takeover

I am currently using the windows on screen keyboard to control the program for the cnc machine. In C# I tried to make a simple on screen keyboard with only the keys I need. I use this command to send key
SendKeys.SendWait("{ENTER}");
or
SendKeys.Send("{ENTER}");
The first click works, but then the background program "takes away" the mouse after the click and then I can no longer control it. While such a problem does not occur on the windows on screen keyboard
Is there a way to avoid this, to make it work like microsoft osk?
Thanks

C# forward mouse events to another window without losing focus

My app screencaptures another window that runs on a second monitor. Now I'd also like to forward mouse clicks made in my app to that window. I tried using SendMessage in user32.dll for this, but this also makes window focus switch, which causes some issues, like the two windows rapidly fighting for focus. Is there are way to place those mouse events without making the hidden window active and losing focus on the main app?
Is there are way to place those mouse events without making the hidden window active and losing focus on the main app?
No, there is not even a way to forward mouse input to another receiver. Messages are only part of the input processing. The system also does internal bookkeeping and you cannot replicate that.
The only reliable way to inject input is by calling SendInput. Doing so doesn't allow you to specify a receiver. Input goes to whichever thread is determined to be the receiver by the system.
Although, more often than not, this question is asked when the problem that needs to be solved is a different one altogether: How do you automate a UI? The answer to that question is UI Automation.

Automatically pop up tablet touch keyboard on WinForms input focus

When I run a WinForms (or Delphi, see at the end) application on Windows 10 in a tablet mode, a touch keyboard does not pop up automatically, when an input box is focused.
I believe that this should happen automatically, without any additional code/setup.
For a test, I have the simplest VS 2015 WinForms desktop application with a single TextBox control.
It's simply the default Windows Forms Application C# project as created by Visual Studio. No code added, no properties changed. Just the TextBox was added, by dropping from the Toolbox (again no properties changed):
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox1.Location = new System.Drawing.Point(64, 27);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 20);
this.textBox1.TabIndex = 0;
To verify my assumption that the pop up should be automatic:
I've tried to run Windows XP version of notepad.exe on Windows 10. It automatically pops up the touch keyboard. I doubt the Windows XP had any explicit support for touch keyboards.
I've also tried some ancient MFC applications (for example FileZilla 2.2.15 from 2005). It also pops up the touch keyboard on all its input boxes. Again, I'm pretty sure, the MFC had no explicit support for touch keyboards either.
The same for applications built on wxWidgets (for example FileZilla 3.x).
It looks like there's something broken in WinForms that prevents the automatic popup. Interestingly, the automatic pop up works:
for (editable) combo boxes (ComboBox with DropDownStyle = DropDown)
for text boxes in a password mode (TextBox.PasswordChar)
for rich text boxes (RichTextBox)
when the input box has focus at the moment the hardware keyboard is "removed" (I test this by flipping the screen on Lenovo Yoga notebook), but never after.
I've seen all the hints about an explicit popup by running the TabTip.exe. E.g.:
How to use Windows On-Screen Keyboard in C# WinForms
Open and close Windows 8 touch keyboard tabtip under desktop
How do I close the on-screen keyboard process from C# winform correctly?
Keyboard Winforms on Windows 10 (surface)
Most of the "solutions" offer a code like this:
var progFiles = #"C:\Program Files\Common Files\Microsoft Shared\ink";
var keyboardPath = Path.Combine(progFiles, "TabTip.exe");
this.keyboardProc = Process.Start(keyboardPath);
But I cannot believe this could be the "official" way. If for nothing else, then because there's no clean way to hide the keyboard opened by running the TabTip.exe (solutions include hacks like killing the process or sending Esc key).
And actually the above hack does not seem to work anymore in Windows 10 Anniversary Update:
Show touch keyboard (TabTip.exe) in Windows 10 Anniversary edition
Interestingly, I see the same behavior with Delphi/C++ Builder/VCL applications. The keyboard does not pop up for edit boxes (TEdit). It does pop up for combo boxes (TComboBox) and for edit boxes in a password mode (PasswordChar). Interestingly not for TRichEdit, what is notable difference to .NET RichTextBox, that maybe worth investigating.
This (unanswered) question describes an identical behavior:
Application written Delphi XE8 touch in edit boxes keyboard not appear in Windows 10.
The root cause seems to be that Winforms' textBox is not an AutomationElement, while the rest of the mentioned controls (ComboBoxes etc) are.
Quoting Markus von und zu Heber's accepted answer here:
We found it in the article "Automatic Touch Keyboard for TextBoxes in
WPF Applications on Windows 8+", but it also works very good (and even
easier!) for winforms. Thank you, Dmitry Lyalin!
Insert a reference to UIAutomationClient.dll to your project
In the form-load-handler of the application's main window, insert the following code:
var asForm = System.Windows.Automation.AutomationElement.FromHandle(this.Handle);
I've been down this road a few times and have only ever been able to implement the taptip.exe option. And in turn close the window by killing the process. I also found out that with some registry hacks you can get the keyboard to default to the handwriting panel if you so choose. But then that only works in Win8 and fails in Win10. Here is what I've done in case anyone else finds this useful:
RegistryKey registryKey = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\TabletTip\\1.7");
registryKey?.SetValue("KeyboardLayoutPreference", 0, RegistryValueKind.DWord);
registryKey?.SetValue("LastUsedModalityWasHandwriting", 1, RegistryValueKind.DWord);
Process.Start(#"C:\Program Files\Common Files\Microsoft Shared\ink\TabTip.exe");
I need to give credit to this post for the registry idea: Windows 8 Desktop App: Open tabtip.exe to secondary keyboard (for numeric textbox)
As far as I can tell, launching osk.exe or tabtip.exe pretty much is the "standard" way of making this work. I've found no "official" solution so far.
However, if it were me doing this, I wouldn't be killing the process or sending keys to try and dismiss the keyboard. Instead, you can obtain the window handle when you launch the process, and use that to minimize the window and hide it from the taskbar.
Someone here has gotten the window handle just to close it, but it gives you the idea: Show & hiding the Windows 8 on screen keyboard from WPF
If you need me to, let me know and I'll see if I can find the time to do up a full example.
As hinted by Ofek Shilon's answer, it seems that the touch keyboard can leverage the UI automation.
One can use implementation of UI automation from UIAutomationClient.dll.
For the UI automation to be magically injected into an application, class initializer of the assembly internal class UiaCoreApi have to be triggered.
On can achieve that for example by calling seeming no-op:
AutomationElement.FromHandle(IntPtr)(-1)
Another way is to implement automation UI explicitly. For that implement the ITextProvider/IValueProvider interfaces for the respective input control.
To bind the implementation of the interfaces to the control, handle WM_GETOBJECT window message with lParam = RootObjectId.
For an example of implementation, see
tombam's answer to How to use Windows On-Screen Keyboard in C# WinForms
or directly poster's article Implementing TextBox with on-screen touch keyboard.
Though interestingly, controls, for which touch keyboard works out-of-the-box (like combo box or password edit box, see the answer), do not implement the WM_GETOBJECT/RootObjectId. There must be a different machinery behind them.
Use a RichTextBox instead of a TextBox control. The RichTextBox supports the touch keyboard and will automatically pop up the keyboard when focus is gained. (similar to other input controls such as the combo box)
The RichTextBox also supports the same properties as the TextBox so it should be a drop in change in most cases. (Both controls derive from TextBoxBase)
I have noticed that if the touch keyboard has been dismissed after it pops up, you may have to tap twice on the control to get it to pop back up.

Capture keyboard input after a hotkey is pressed, without app focus

I have a bar code scanner that presents itself as a USB-HID keyboard.
How do I capture scanned bar codes in my c# WPF application, if the app currently does not have focus?
When a barcode is scanned, the scanner sends one or more configurable prefix keypress events, then the barcode itself, then some postfix keypresses.
If the application has focus, I can use the prefix hotkeys to focus a specific textbox; then the barcode is automatically entered into this textbox.
If the application does not have focus, how can I receive the barcode?
I could register a global hotkey via pInvoking RegisterHotkey from User32.dll, as outlined e.g. here:
How can I register a global hot key to say CTRL+SHIFT+(LETTER) using WPF and .NET 3.5?
Global hotkeys in WPF working from every window
But how do I capture the following keyboard events containing the bar code?
I am a little dubious about this approach as well, is there a simpler way to capture a string prefixed by a specific hotkey, without having focus?

Control OS Mouse Event using Windows Form - C#

The program I am trying to do is to simulate the mouse event of a operating system using keyboard with Windows Form. Right now I am able to change the cursor and do different actions like mouse clicking inside the Form (when the Form is on the Top).
The problem is I would like to extend it to the whole Operating System, which means even if my Windows Form is not at the top, I am still able to control my cursor and do all sorts of mouse event on other applications while the Form is running. How should I do to implement this ?
You might want to look at this library Global System Hooks in .NET which uses global system hooks to detect all mouse and keyboard events include those outside of your application.
You can synthesize OS-wide keystrokes, mouse motions, and button clicks using the Win32 SendInput() API. You can call it from C# using P/Invoke. Sample code can be found here: SendInput on PInvoke.net.
I remember back in the day I used the SendInput (and a screenshot API) to create a Minesweeper bot in C# (2.0 I think). It could solve an Expert puzzle in about one second. I wish I still had the source code to sample here, but I don't.
EDIT: It appears someone has already created a nice .NET warpper for the SendInput(): Windows Input Simulator on CodePlex.

Categories