Handle button click in another application - c#

I am looking for a method to get a button press event, something similar like this which is used to retrieve a pointer right after an event is fired. The button I mean is located on any particular windows application I set as a target (.eg windows media player's Play button). If you know what message to catch right after I click the Play button.

What you are looking for is Global Mouse Hook. You can take a look at this article: Processing Global Mouse and Keyboard Hooks in C#
Once you handle CLICK event, you can use API functions to get Application / Control under the cursor (WindowFromPoint).
Here is one more article with code sample that will help you: .NET Object Spy and InvokeRemote (this is similar to spy++)
You cannot do this without using windows native methods.

User tagged this in winapi. He probably already knew about hooks. Part of HABJAN reply point to the right direction. Answer to the question is: Use Spy++
I don't think there's a message for "button clicked" or "url link clicked" but if you observe a program with spy++ you'll see there's UI notification messages like these ones: WM_CAPTURECHANGED, WM_CHANGEUISTATE or even WM_POSCHANGING. I think these are the messages you are looking for.
Another way to do this may be to use the AutomationElement, see this answer: Getting the current tab's URL from Google Chrome using C#
PS: If I could downvote an edit by another user to a question, I would downvote Dyppl's edit... question used very precise words: "get message" instead of "handle" in the title.
PS2: It's an old question but I was asking me the same question earlier and came across it on google, maybe my answer will help someone

Related

Determine which mouse trigger the Click event in multiple mouse using C#? [duplicate]

I want to detect when my touchpad is clicked!
I normally use a usb mouse, so I don't use the touchpad for anything. Instead I'd like to make it possible to perform an action in .NET, when the touchpad is clicked. This way I can use it as a shortcut: One tap and something cool happens.
Is this possible, and if yes, any clue how? I'd prefer if it could be working in VB.NET or C#.
My theory is that I'd have to make a mousehook, which then somehow determines which device the click is coming from. If the click is determined to be from the touchpad, then cancel the click and doWhatever().
Thanks!
* EDIT *
Well, it's "solved", sort of :) In a odd coincidence, Synaptics released their latest driver and software for their touchpads a few days ago with some new functionality. As my laptop has a synaptics touchpad, I tried out the software and interestingly enough, the functionality for designating clicks on the trackpad to perform an action, was built-in.
So the desired function has been achieved, without a line of code (my own code anyway :).
Answer goes to Adrian though for the link to the RawInputSharp library. I tinkered with it yesterday, and I'm 90% sure it would be possible to use for this purpose, in the event a laptop doesn't have a synaptics trackpad.
Have a look at the RawInputSharp library from this page. It uses pInvokes into User32.dll to get hold of input device information. Using it you can detect which device (ie. mouse) input is coming from.
Having had a little play with it, I managed to extract some code that displays just the device id - a different value depending on whether I use my USB mouse or my internal touchpad. The tricky thing would be to identify which device id is that of your touchpad automatically, but you could manually configure this in your application.

On-screen keyboard in Windows Universal App on Windows IoT?

Is it possible to show the On-Screen Keyboard (osk) in a Windows Universal App on Windows 10 IoT Core?
The raspberry pi only has a mouse attached and when I click on a textbox I'd like to show the osk.
I can set the InputScope and IsTextPredictionEnabled but can't figure out how to trigger the osk to show.
There is an example here of an on screen keyboard. If you look at the sample they are using one in their web browser. This is a pretty good keyboard but it would be better to have the default windows keyboard
https://github.com/ms-iot/samples/tree/develop/DigitalSign
If it was up to me I would prefer the default OSK since you can specify slightly different keyboard layouts based on inputs. I don't like this one because the text box loses focus which can limit the use of action listeners that you can use. Hopefully this is something that gets enable in future builds but for now this is a pretty good keyboard you can steal so you don't have to start from scratch.
On a side note you should upvote in Windows Feedback under "Developer Platform/ Windows 10 IoT Core". There should be a suggestion open.
I asked that question back in July on the MSDN forums. The answer that I got was:
"This has been answered here:
https://social.msdn.microsoft.com/Forums/en-US/d81ebfa0-ccdc-479e-8e2f-bc921de17343/onscreen-keyboard?forum=WindowsIoT
There's no onscreen keyboard available at the moment. There's the possibility that one will be added later, but it's not guaranteed. If you read the linked post you also find an explanation as to why none is available."
Just for reference, here's the link to the MSDN question that I asked: https://social.msdn.microsoft.com/Forums/en-US/9c95f424-f0ca-4751-9497-a019c1d40907/touch-keyboard?forum=WindowsIoT
And here's the answer from the linked post:
"At the moment there is no On-Screen Keyboard available. If you want a user to input something via the screen then you’ll want to provide the UI for that input in your application. Part of the reason for this is that we don’t have a ‘shell’ like explorer that is there helping control what app is running or providing other services such as SIP / OSK as is handled on the desktop.
Longer term I believe this is something that the product team is considering / looking into but I do not have any timeline on if or when it might be an option (if ever). If I learn otherwise I will let you know.
I hope that helps."
As mentioned in this answer, if you have the latest version, check On-Screen Keyboard from the Windows Device Portal.
From https://social.msdn.microsoft.com/Forums/en-US/d81ebfa0-ccdc-479e-8e2f-bc921de17343/onscreen-keyboard?forum=WindowsIoT : -
There is one available (I think since 14986). You have to enable it in
the device portal under "Device Settings" > "On-screen Keyboard".
Thursday, January 05, 2017 12:57 PM
I wanted to post the workaround that I came up with, but I didn't have enough rep points yet to include more than 2 links.
So I'm posting it as another answer.
Here it is:
And for those interested, I wrote up a very basic example of a osk-UI element. The Idea was to create something that could be reused if necessary. It's also not the fastest thing but maybe it will give you ideas.
Its basically a UserControl inside a Popup. When a Textbox gets focused, it fires an event that sets the "Keyboard Popup"'s DataContext to that of the Textbox and shows the "Keyboard".
I dropped it into a .zip file and uploaded it to my Box.com account.
Here's the project folder: https://app.box.com/s/vsf1wclaivsan4hhu2u2qdrb2awnbspu

Determining (programmatically) who controls the mouse on a PC using C# or C++

Is there a way to determine who is controlling the mouse (and which mouse) on a PC programmatically? I recently installed LogMeIn (logmein.com) and wanted to know if it's possible to (1) tell within a program if the mouse is being clicked/moved by the direct user or by a remote user, (2) write a stand-alone program that simply shows mouse events (on any application) and whether or not the mouse event was generated by a local or remote user. I am somewhat familiar with Win32 hooks, but don't think that they can give this sort of information. Regarding (1) it would seem like a common request. I.e. "Only allow user to complete button presses related to password change if he is local..." or something like that.
Of secondary importance (just academic interest actually) is the question of telling whether the local user is using the mouse or the trackpad.
you can hook the device API's of windows that you want to get the information from, then if the mouse moves the api is probably not called when the user movers the mouse, but if logmein does then it probably calls some SetMousePointerPos-like C WinAPI.
Since posting, I came across this post which basically answers the question.
C# Get Mouse handle (GetRawInputDeviceInfo)
I'd add that for those of you who want to use WPF instead of WinForms (as the example above uses), check out ComponentDispatcher.ThreadFilterMessage (for WPF) or IMessageFilter (Windows Forms).
I wrote a couple of programs based on the above posting (one is basically the posting above with some minor additions and the other is a WPF (as opposed to WinForms)). If I can figure out GitHub I'll post all the code and add a comment here. But the posting above definitely gives you all you need.

how to Make a program intangable with c#?

I just wanted to know, if there is someway to make a program (or part of a program) intangable with c#. I want to make it so that people can see the program is there, but if they were to click, it would click whatever is underneath it. I would also like to know if you can do that backwords. Is there someway to make an item that is invisable, clickable?
Thank you for your help!
To your vague question, I offer a vague response:
Sounds like your option one is possible. You would need to send the click event (message) that you receive to the appropriate window (the one underneath yours). I suspect that you would have to DllImport some stuff to do this from c#.
Your option two, while more difficult, is probably also possible because you can inject code into other running executables. This will be a privileged operation, and you will likely again have to use stuff from non .NET dlls to do it from c#. See Three Ways to Inject Your Code into Another Process on CodeProject.
If you want to display something to a user without it getting in the way of whatever it was they were doing at the time you could pop up your messages in bubble from the task bar perhaps?
The answer to this question covers this. Or if you're lazy here's the code project link.
Ok so it sometimes might be necessary to show something on screen and not let it be clickable (like On-Screen-Display for video playback to show volume increase, etc..)
Here's an example of how to this in C# - from codeproject: http://www.codeproject.com/KB/cs/OSDwindow.aspx
This uses the Win32 API ShowWindow(hWnd, SW_SHOWNOACTIVATE) to present the window without losing focus (can't be selected).
And here's the MSDN page for this call ShowWindow
To display a window that is invisible but clickable you can use a window with no border (FormBorderStyle=None) and set transparency to 1%.
Hope that helps!

Send keys to WPF Browser control

Can I programatically send [UserID]{TAB}[Password]{CARRIAGE RETURN} to a webbrowser control which has a userID, password and Sign-in button there. I wanted to use my own virtual keyboard in my application. Any tips here?
Sorry for the late answer but I've just finished a similar project and as part of the work am in the process of open sourcing two projects to Codeplex.
The first is the Windows Input Simulator which is a simple .NET wrapper around the Win32 SendInput written in C#.
The second is a very customisable on screen keyboard or touch screen keyboard control and toolkit called WpfKB and will be available as an initial release tomorrow. Hope these are of help to you or anyone else who comes across the projects.
I recently had to implement automatic authentication through a WPF browser control, and I looked into simulating keystrokes. I didn't need a full virtual keyboard so interacting with the DOM of the login page through IHTMLDocument2 ended up being the best approach, but I looked into keystroke automation before making that decision and found a few options.
You can raise the appropriate routed events on the control as described in Simulating basic keyboard events and Simulating text input. I don't know of any specific problems with this approach but I opted against it simply because I wasn't comfortable simulating input without looking at how the CLR handles the actual input, and without at least raising the complete lifetime (PreviewKeyDown, KeyDown, PreviewKeyUp, KeyUp) I was wary of unintended consequences.
Take a look at WOSK on CodePlex. It's a good example of how to invoke Win32 keybd_event and SendInput functions to generate the low-level input messages via Managed Windows API to simulate input. There's some unnecessary fluff (eg transparency) and some odd WPF usage, such as using a CommandParameter with a Click event instead of a Command on the buttons, but the general approach is sane and it's reasonably complete.
You can also invoke the windows on-screen keyboard as alluded to by Jeroen. I didn't try this because I didn't need a virtual keyboard, but if you're going to call into Win32 anyway, you might as well follow the WOSK model and build the UI the way you want it.

Categories