I would like to get the previous entered Text message in an open notepad instance until the ENTER key is pressed (asynchronously).
That is, enter "some text is entered [ENTER]" then I like to get the string "some text is entered" (MessageBox.show(sometext);) Would you please offer me an idea of how to do this without use of a global keyboard hook ? Thank you
If keyboard hook does not work - consider spying on the messages sent/handled by the notepad window. Spyxx tool from Visual Studio tools will show messages (note to match 32/64 bit to be able to spy).
Note that it still requires ability to run in another process -
How can my application read windows messages (WM_SETTEXT) from another application? , I would not recommend native code for doing it instead of C#.
Related
The requirement is that when I launch the "Notepad" by my application button and keep the "Notepad" in focus, then I scan a bar-code using my bar-code scanner. There will be a string in the "Notepad" and the string is captured in my application in background via USB port.
Now the keyboard codes were captured by my winform application in the background, but I can not parse the keyboard codes to the real strings scanned by the scanner. For example, I got a real "abcDEFghi123" in the "Notepad", but I got a keyboard strings like "ABCShiftkeyDEFShiftkeyFGHID1D2D3" in my winform program and I got the strings"ABCDEFFGHI123" in the textbox control. So could anyone help me to parse the keyboard code strings to real strings "abcDEFghi123"?
The Scanned barcode and real strings
And the sample code is
My sample code developed by visual studio 2015
Any advice and suggestions will be greatly appreciated.
Why make the detour over Notepad? The Barcode Scanner should be able to send the keycodes to a text filed inside your application as long as you give it focus before scanning...
If that's not an option then you should be able to use KeysConverter to get the right character. (Can't test it right now)
Failing that you should be able to use additional properties of KeyEventArgs like Shift to determine if a character is upper or lower case.
Summary
How do you prevent the shift-key, which is part of a global hotkey, from interfering when sending text to the active window in Windows, by calling System.Windows.Forms.SendKeys.Send("abc") from another process when a shift-containing-global-hotkey is activated?
The problem
The window which is active when the hotkey is activated misinterpret the text sent to since the shift-key, which is part of the hotkey, is still pressed while it receives and process the the text sent to it. It is humanly impossible to release the shift-key fast enough so it is still not pressed when the text is received.
It is not possible to change the hotkey to not contain shift, and even if were possible, the ctrl-key would interfere with the processing in a similar way.
The sending application is run as a normal user without admin privileges, and UAC is enabled.
There is a background application running in windows. It is a normal .NET C# WinForm application, started by the user and running without a visible GUI.
The background application has registered a global hotkey, that is, a hotkey that can be pressed anywhere in Windows, no matter which application is currently active.
The hotkey is: <shift>+F9 (RegisterHotKey(hWnd, hotkeyId, 4/*MOD_SHIFT*/, 120 /*Keys.F9*/);
When the hotkey is activated, the background application calls System.Windows.Forms.SendKeys.Send("abc")
The active window receives the text "abc", but since the shift-key from the hotkey is still pressed, the result end up as "ABC".
The question
What are the possible ways to make sure the text sent end up the same after the receiving window get, process and interpret it?
That is, when sending "abc" to a running instance of notepad.exe by pressing <shift>+F9, the text showing up in notepad should be "abc" and not "ABC".
As far as I can tell,
Check the Shift modifier status, and only send your keys after you verify that it is not pressed
Don't use SendKeys
I think that #1 is pretty self explanatory, but keep in mind that even if you verify that Shift is not pressed before you start sending keys, Its possible that the user can still press Shift or another modifier while you are sending keys, or even worse its possible for the active window to lose focus and stop it completely. If you're designing a program that simply inserts user-defined text after a hotkey press and the user is expecting it, then this is not a big deal and is the appropriate way to to this.
You have a few options for #2, I'd suggest looking into using SendMessage with An apropriate message (WM_CHAR, WM_SETTEXT, WM_KEYDOWN, or etc) to send a message directly to the window in question.
After everything is said, its important to realise that this is a really uncertain process. You can never guarantee that simulating keyboard inputs or sending key messages will register as you would like them to and it may largely depend on the application your sending messages to (esp in the case of SendMessage).
I want to create a simple C# application that will listen to another application for a certain keyboard press or event. For example, I have an excel file that I want to monitor that if when a user will press the "a" key, my C# program will show a message box saying "you press the letter a". Aside from that, I want my C# program to listen to that excel file only.
I've heard about the hooking thing and I don't know where to start, like the very basic of it or is there any other alternate solutions?
Any reply would be much appreciated! More power!
I have a webpage that requires users to enter their credentials in a 'Connect to "ServerName"' dialog box and click OK. I want to automate this login proccess using C# or PowerShell. Is this possible? If not, is there another way of automating this?
Looks like you are trying to execute the webpage automatically and that popup is blocking you from doing that, if that the case you can use SendKeys to type in the boxes
Also a macro recorder being fired by a scheduler task will do it.
you can also instead of trying to open the webpage make a WebRequest from a C# application passing the credentials , that will lead you to the same results...
regards
It is possible, but it is probably far easier to use AutoHotkey.
It can be scripted to watch for a window of a certain name and then send keystrokes to that window automatically. Alternatively, you can set up the script so that the user just has to press some key or series of keys in that window and it will be converted to a set of keystrokes.
The download also includes a compiler, so once you get your script right you can compile it to an .exe to distribute.
Just the functionality of intercepting the keyboard entry , transforming it and sending it back is required.eg if user press a i wold like to send e etc.
To make a application that accepts the entry from keyboard modify it and send it to the active window (may be application like word, excel,notepad windows screens ) etc.
The feature is just like google's transliteration application for windows .
I would like to do it in c# .
I think this involves hooking in the keyboard and sending keys (like sendkey in vb) to the current window.
The working is just like Google transliteration .However the logic for transformation is quite different.
I'm not sure what the question is exactly, so it may get closed for being too vague. However, it sounds like you'll have to buffer all of the keystrokes, send words or phrases to some service asynchronously to be transliterated and then push them back into keyboard buffer with something like sendkeys as you mentioned. The problem is that if your app has focus, we don't know where the user wants the transliterated text to show up, and if the app such as Word has focus, it will receive both the real keystrokes and the transliterated text, which will then be mixed together (a mess). Seems better just to transliterate chunks of text rather than trying to interfere with the keyboard buffer - besides, backspace, undo, etc. will be nearly impossible to honor.
You need to write a dll which uses Windows Text Service framework or legacy IME framework .
This dll will get loaded into langauge bar and when you will activate it ; your code , which has implemented a set of APIS required by TSF or IME framework , will get the key event , You can then pass it to your code which translates it and passes a string back to you.Another of your implemented API can then input to the Current text editor of the windows Application.