Click here to Skip to main content
16,016,306 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Is there any way to detect text cursor icon anywhere. Like I am writing in this text field, my program could detect that text cursor is enabled here, and possibly generate any event after detection.
Posted
Updated 13-Apr-15 20:06pm
v2
Comments
Sergey Alexandrovich Kryukov 13-Apr-15 14:50pm    
What do you mean by "text cursor icon". There is no such thing, is there? :-)
And be certain: is it about ASP.NET, WPF, or Forms? The answers on each would be unrelated to each other.
—SA
Member 11485559 14-Apr-15 1:48am    
Thank you for your response.

To be very precise, I have a keyboard which I need it to get visible only when any text field is in focus. That text field could either be anywhere, in forms, text fields in websites etc. I am sorry, if my question confuses you, it actually confused me as well, as I need something like that.
Sergey Alexandrovich Kryukov 14-Apr-15 2:13am    
No need to be sorry. Your idea of the behavior of your virtual keyboard is excellent (so I up-voted the question), but there are problems with it. I am not sure that such detection can always be possible, by the reason I explained about WPF, and hence, some other cases of applications. However, you can try my answer...
—SA

There is no cursor as you describe it. The "text cursor" is really called the "caret", or insertion point. It's just a line or other symbol that is periodically drawn and removed to show the user where the next character is going to be typed. There is no global event or other mechanism that notifies when are where the symbol is drawn.

You can however get the window that currently has the input focus with GetForegroundWindow[^] and then call GetCaretPos[^] to get the caret position in the client window coordinates. This can fail if the current window doesn't have a caret.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 13-Apr-15 14:54pm    
No Dave, it won't work with in all cases. For example, it could be some WPF control withing the window; such controls are not windows, so Windows API won't work on then.
This question, with the tags "ASP.NET", "WPF" and "WindowsForms" just does not deserve to be answered, perhaps except for your first two sentences...
—SA
Dave Kreskowiak 13-Apr-15 15:04pm    
Oh, I know that. I didn't notice all the tags he put in and just assumed Windows Forms.
Sergey Alexandrovich Kryukov 13-Apr-15 15:08pm    
Of course you knew that. I did not pay attention for those tags, too. We often don't see something we don't expect to see...
—SA
Sergey Alexandrovich Kryukov 14-Apr-15 2:23am    
Besides, there is no a need to get foreground window...
—SA
Member 11485559 14-Apr-15 1:50am    
I am sorry, if my question did not deserve to be answered and wasted your time, but I have a confusion which I have written above in SA's comment. I hope you understand that. Thank you
You can try to use: https://msdn.microsoft.com/en-us/library/windows/desktop/ms648402%28v=vs.85%29.aspx.

With C#, you can use it through P/Invoke: http://www.pinvoke.net/default.aspx/user32.getcaretpos.

Note that if some control with the caret is in focus, this function will return true, otherwise it will return false, so you can hide your keyboard.

Note the difference with Solution 1: there is no need to call any other function. This function gives you current caret position (if any) global to the desktop.

On little problem is: some applications can handle keyboard input, but not use "official" Windows caret API. But you can try it out to see how often it might be the case. For such cases, you can have an option to disable your auto-hide feature.

As to the virtual keyboard itself, it can have certain problems; so please see my advice on this topic in my past answers:
Application focus getting and losing,
Programming on BACKSPACE button,
Creating a Virtual Keyboard.

—SA
 
Share this answer
 
v3
Comments
Member 11485559 14-Apr-15 3:05am    
Thank you Sir for responding once again and providing me with fruitful links. I have already developed a keyboard and it's working fine with focusing. It has the top most layer without getting focused. Now, I am trying to auto-hide my keyboard as it will be more user friendly. I am looking forward to all the links you provided and will get back here soon. Thank you :)
Sergey Alexandrovich Kryukov 14-Apr-15 10:15am    
Sure. Will you accept this answer? I think this is all you can do about it.
In all cases, your follow-up questions will be welcome.

(Yes, using topmost but non-focusable window for the keyboard is the key.)

—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900