C# Sending CTRL+C keyboard input to Word - c#

I'm currently practicing hotkeys and writing a small background app that instantly googles the selected text on the ctrl+shift+alt+N hotkey.
It all works very well. I register the global HK, I intercept the message via WndProc() etc.
What does not work is the way I'm trying to copy the selected text in the currently focused window by sending it a CTRL+C keyboard input.
Here is my current method, in a nutshell:
IntPtr fWin = GetForegroundWindow();
SetForegroundWindow(fWin);
//InputSimulator.SimulateModifiedKeyStroke(VirtualKeyCode.CONTROL, VirtualKeyCode.VK_C);
SendKeys.SendWait("^(c)"); //I tried both ways, with the InputSimulator lib, and the standard SendKeys. None work.
Thread.Sleep(1000); //I wait a little bit for the clipboard to get the text.
Console.WriteLine(Clipboard.GetText()); //This is where I get disappointed...
Process.Start("firefox.exe", "-new-tab http://www.google.com/search?q=" + Clipboard.GetText().Replace(" ", "%20"));
This method works for some applications only (like the one we use in the office: Trados Studio which is quite the complex text management software). However, I'm unable to make it work with applications like Word, Outlook or Notepad.
Believe it or not, I'm able to send them letters like this:
string txt = "Y U NO WORK?";
SendWait(txt);
//OR
InputSimulator.SimulateTextEntry(txt);
But when it comes up to CTRL+C, nothing happens.
Do you guys have an idea of what's going on? I hope there isn't some MS restrictions on this one... Thanks in advance!

I'm not able to help you directly but may I suggest that you read about OLE/COM.
https://msdn.microsoft.com/en-us/library/19z074ky.aspx
I know that you can use this to accesses word, excel etc from other applications.

The solution was to make sure that the keys were resolved and released before sending CTRL+C input to the focused application to avoid Hotkey overlap. Otherwise, the focused application is sent a messed up HK that it doesn't understand or handles weirdly.
It would explain why Word inserted a date or inserted character "i" or copyright instead of copying to clipboard.
Hope it makes sense!

Related

Robust SendKeys solution required

I'm trying to write an application that can reliably send key presses to another application.
The second app has a text field which opens when 9 is pressed.
Text can then be typed and then the text field is closed when Enter is pressed.
If I use SendKeys to try to do this the field does not open if I send 9, but if the field is already open my tool can send text ok, but then it does not close if I send an Enter.
I'm guessing this is because a lower-level interaction with the keyboard driver or similar is being used.
Is there a reliable way I can simulate actual keyboard input to the application from another C# app?
I have found a few potential solutions online but these are generally incomplete with either missing references etc. or missing code elsewhere.
I will continue to search and I will post the solution here if I find it!
is it an option to invoke on screen keyboard provided by os. your app, calling app will loose or cant track the key events, however if you open your second application and invove osk.exe, then the purpose of robust keyboard sending keys to an app could be fullfilled.
I explained this here
EDIT: ignoring on screen keyboard
You can try using RegisterHotKey to open the textpad on a specific sequence of key(s) and persist the handle in a instance variable. On the sequence of second hot key(s) you can close the textpad (you already know the handle)
there is a video explaining this

C# application to control another application

I have got a problem when I try to control another Windows application using my own C# application.
The requirement is like this. The "another application" is a business software which our company have bought. We want to develop an application to fill text boxes and simulate mouse clicking. All text input is from a certain XML file.
I have tried to approach such function with SendMessage and SendWait function call. Problem is in some cases or at some end user's laptop, the text filling is not correct. Meanwhile, the mouse clicking will be fail in some other cases.
My imagination of the reason cause is synchronisation problems with call SendMessage. But I have no idea how to fix it. Is there any suggestion or solutions?
Looking forward your reply.
SendMessage and SendWait will always have those sorts of issues (especially app-to-app), e.g. the user clicks someone on the screen at exactly the right/wrong time. Really the only way I know to reliably do this is by hooking onto the mouse/keyboard and take total control... its hard and painful.
Note: there are plenty of professional macro'ing software packages on the market (faking keyboard input, mouse movement, clicks etc). Some are very sophisticated with their own programming language and will possibly do what you want. Google "windows macroing software" and you will find heaps - I recommend you do you research, trial several packages til you find one you want and then purchase it.

Can use clipboard to add characters to Note Pad immediately after keystroke?

I am coding a keyboard software to display vietnamese for text editor such as Words or Note Pad, I wonder can I use clipboard to display clipboard contents immediately to active windows such as Word Pad
I think you are asking if you can use the clipboard to push data into the active window of an app such as Notepad. You can do this, in conjunction with sending keystroke messages. I mean, it's possible. It will probably work if no other applications are running on the system. i.e. if the user has your app running, notepad, and nothing else. i.e. a laboratory situation. In the real world, this breaks down and becomes a terrible idea.
You are going to cause lots of conflicts with any other application that monitors the clipboard. Keep in mind that only one app can have the clipboard open at once. So as you shove data onto it, and then tell Notepad to paste, you've got to not only allow time for Notepad to do the pasting, but you also have to account for any other app that's signed up to receive clipboard notification. (such as my own ClipMate, or other clipboard-aware apps such as Microsoft Word, Explorer, etc..)
The clipboard is provided for the benefit and convenience of the user, not the programmer.
Summary: yes it's possible, but no, you should not do this.

how to disable "PRINT SCREEN" button while running my Application in WPF?

How can I disable Print Screen functionality while my WPF application is running?
The use-case is that my client wants to avoid unnecessary replication of valuable patient-centric data from the outside world and they provide the physical security to keep people from taking data through non-digital means.
Okay, it is possible, and could indeed be useful if your application is deployed in an environment where a camera is not available to the user.
First of all, I used the RegisterHotKey and UnregisterHotKey API calls, documented here http://pinvoke.net/default.aspx/user32.RegisterHotKey as described in this rather old article here http://msdn.microsoft.com/en-us/magazine/cc163713.aspx.
I registered the IDHOT_SNAPDESKTOP hotkey in the Window_Load event and unregistered it in the Window_Closed. Trying to do this in the constructor gave me problems getting a consistent handle with the WindowInteropHelper(this) method.
If you'd like to do more than just ignore the keys you can set up a windows message handler, making a kind of WndProc using,
HwndSource source = HwndSource.FromHwnd(<handle>);
source.AddHook(<WndProc>);
making the handle as described above, and the WndProc implementation yourself.
As yet, I don't know how to "not" handle the hot key and get windows to perform its normal behaviour except, of course, by unregistering the hotkeys.
Its not very elegant or "WPF" but it worked for me.
As #ghord comments
The use of EnsureHandle() looks useful for getting a handler in the constructor.
It's not possible to disable printing, and even if it were possible, it would be easily circumvented by a cell phone camera. Many are in the megapixel resolution range, making it quite easy for someone to get the information they want.
If you want to disable the Print Screen Key on your keyboard, Jodrell's answer gives a way of doing that (understanding that it's not going to keep people from printing, and a determined user will find a way around that).
Really, it all comes down to trust. If an employer can't trust their employees not to remove data that is already protected by law in most jurisdictions (HIPAA in the USA), then there's a bigger issue at stake.
Easy:
Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().IsScreenCaptureEnabled = false;
Simply speaking, you cannot. "Print screen" just copies the pixels on the screen to the clipboard, and is not part of your application.
Basically you can hook to the ClipBoard events and then set the image copied to null if someone does it. So they can copy the image but it will be reset:
Have a look at this:
Clipboard event C#
Alternatively in a timer, check the content of the clip board and clear it as soon as it is set to a picture.
No, No way to do that. Even if you capture the Print Screen key in your application user might set focus to some other application and then do the Print screen(having your application on side etc.).
Only way would be to create a dummy application in background which captures all keystrokes using Keyboard Hooks and filters Print Screen, but that will happen for all applications not just yours. And moreover as George said user can use cellphone camera too!
I think Microsoft Rights Management System can help. Give it a try. Following is the link:
Microsoft Rights Management System
The only way I can think of is to use the native Windows API (SetWindowsHookEx) to catch all keystrokes and filter out the PrintScreen key. However this would involve creating a native (i.e. unmanaged) DLL to actually do the keystroke processing.

Setting Pidgin status from .NET

Is there a practical way to set the global status message in Pidgin for Windows from .NET?
Dbus is not available in the Windows version of Pidgin.
Searching SO and the net has revealed helpful tips such as "rewrite libpurple in C#", which might be a bit beyond my time/enthusiasm level for this project at least...
Thanks
how about writing a plugin, and have it expose a way for your other program to pass the status to it. Could be a file that you write the status to, or maybe a local tcp port (if you can do that in plugins).
Have you considered simply simulating keyboard input to the Pidgin window using C#?
You will need to find the Pidgin window programmatically and then set your status by sending simulated keyboard input to that window handle.
A good Microsoft example of how it was done with Calculator:
http://msdn.microsoft.com/en-us/library/ms171548.aspx
As I recall, Pidgin will set your status if you simply put the main window (buddy list) in focus, start typing, and then press Enter. Simulating this keyboard input should be quite straightforward.
A dirty solution (that future versions of Pidgin might break), yes, but certainly MUCH easier than writing a plug-in or making your own libpurple wrapper.

Categories

Resources