Click here to Skip to main content
15,901,666 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am writing a program that displays keyboard shortcuts (hotkeys) in C# and WPF for use when making video tutorials.

Everything I have done so far, works pretty good. The problem is, sometimes I don't want to process keyboard shortcuts, ie: don't display them.

That would be if the user is typing something into any kind of editable textbox or element where a caret is blinking.

I found a solution here that kind of works:
Getting Caret Position Inside Any Application[^]

I don't need to know position or anything other than a cursor is active and blinking somewhere on screen. Then I would ignore showing the keys being pressed and not show them on screen. The problem with the above code is, it works in some apps but not others. In 3dsmax all works correctly, in Maya, I have to click the input box a 2nd time and it will work, in other 3d programs like cinema 4d it doesn't work at all.

Is there anyway to to detect a blinking caret / active caret in an external program using C# and WPF ?


Update 3/31/23
I saw someone posted a solution, but must have deleted it - I did try it cause the solution remained visible on my phone but not on my desktop - regardless the solution didn't work.

2nd: As stated above, my version works mostly, but even my version doesn't detect the bright blinking caret in visual studio. I am assuming if I can detect the caret in visual studio, it might detect for all (if this helps people come up with a solution)

3rd: I have a feeling its a window depth problem, ie: the code area of visual studio isn't detected as the foreground window, only the surrounding frame is the foreground window (if you get my drift). Maybe the problem is detecting the caret in a child window of the foreground window ?!

What I have tried:

I have tried:
public void GetCaretPosition()

    guiInfo = new GUITHREADINFO();
    guiInfo.cbSize = (uint)Marshal.SizeOf(guiInfo);

    // Get GuiThreadInfo into guiInfo
    GetGUIThreadInfo(0, out guiInfo);


private void EvaluateCaretPosition()

     // RJA - This method is used to determine if user is trying to type text
     // RJA - if so, we wont show it as shortcuts
     caretPosition = new Point();


     caretPosition.X = (int)guiInfo.rcCaret.Left + 25;
     caretPosition.Y = (int)guiInfo.rcCaret.Bottom + 25;

     if ((int)caretPosition.X != 25 || (int)caretPosition.Y != 25)
         Globals.bCaretActive = true;
         Globals.bCaretActive = false;

     txtCaret.Text = ((int)caretPosition.X).ToString() + " / " + ((int)caretPosition.Y).ToString();

Updated 31-Mar-23 2:49am
[no name] 30-Mar-23 22:00pm    
Perhaps your (I assume) short cuts are not distinct enough; e.g ctrl+..; alt+..; etc. I don't see why a textbox s/b an issue. (i.e. preview the keyboard input instead of looking at the carat)
Robert Andersen 2022 30-Mar-23 23:46pm    
Hi - The idea is to display descriptors to the shortcuts - but some shortcuts are normal typed things like a to z or 1 to 9. If the user is typing these things to say, name an object or enter a size, then its not a shortcut, its text entry. Then I want my program to not show the descriptors. If I can determine if a caret is active, then I know its text input and not a shortcut and I want the program to automatically detect this if possible.
Graeme_Grant 31-Mar-23 8:45am    
He posted a ChatGPT answer. Typically, those answers are not correct. Many YT examples cover the unreliability of ChatGPT - it gets more wrong than right. When he saw your reply, he realised this and deleted his response. He is still posting ChatGPT answers to other posts.
Robert Andersen 2022 31-Mar-23 8:50am    

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