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.
Related
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
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!
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.
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.
I know something about MACROS. I don't mean the ASSEMBLY language kind. I am talking about those programs that you can use perform repetitions actions on another program. I am talking about those programs that you can use to record a series of events on your computer, like, mouse movements and button clicks and then you can play them back. Some of them are elaborate enough to run only on a paricular app that you designate.
I wrote one of sorts once. It was a program that launched an Excel sessions and then used the dynamic data exchage pipe of some kind to feed the excell session script commands. It worked.
But something on the level of the operating system, I imagine, is a whole different story.
How does someone go about writing a "macro" in C#?
I think the approach I will take is to use the spy routine that comes with the development environment to get a list of the proper messages and parameters (wm_lbuttondown for example) and then use dynamic data exchange to send those messages to the app.
So I have three questions.
Is this the best way to do this?
How do I get a handle to an app that is already running?
How do I send user-like messages to an app that is already running?
There are different answers based on many following factors:
is it 3rd party or your own
application?
does it have automation interface
GUI toolkit used in app
If it is a 3rd party app then you need to work on Windows API level via PInvoke - subclassing WinMain proc, capturing and sending input messages, etc. There are 3rd party library for that task. C# obviously is not a right choice for such task.
In case application has automation model (like Excel) it's a pretty straight forward to write program that will be interact with this app.
If it's your own application you want to enhance with macros functionality then you should take this into account on design state. If you use Command pattern from the beginning then it's not hard to program macro recording.
You should provide more details to get a better answer.
Oh, I almost forgot to answer those three questions
Is this the best way to do this?
Depends on concrete scenario
How do I get a handle to an app that is already running?
Depends on application. If it's a native Win app you can easily get process Id and window's handle via WinApi.
How do I send user-like messages to an app that is already running?
Once again it depends on application type. For native win apps you can easily send WM_XXX messages via WinAPI
Unless its something you need to add in your own program you can just download a keyboard/mouse macro program and use it to perform these repeatable actions.
On the other hand to perform macro's in your own program you would want to find a way to record the buttons clicked and write them to a temporary list that can be saved and then run the list by clicking the buttons (programmically).