title: What is the correct, modern way to handle arbitrary text input in a custom control on Windows? WM_CHAR? IMM? TSF?
body: <p>I want to be able to support text input in a custom Windows control, just like the EDIT and rich edit controls already do, but not subclassing either of those. The control currently uses Direct2D and DirectWrite to draw text, and runs on Windows Vista SP1 with Platform Update or newer (I might change it to Windows 7 SP1 with Platform Update or newer if I decide I need newer Direct2D and DirectWrite features, assuming those are available there or on Windows 8 only, but that's a different question...)</p>
  
  <p>For what it's worth, on OS X I would use <a href="https://developer.apple.com/reference/appkit/nstextinputclient?language=objc" rel="nofollow noreferrer">NSTextInputClient</a> and on GTK+ I would use <a href="https://developer.gnome.org/gtk3/stable/GtkIMContext.html" rel="nofollow noreferrer">GtkIMContext</a>. It's things like these that I'm talking about.</p>
  
  <p>The obvious choice is to use <code>WM_CHAR</code>, which if I gather correctly, is natively UTF-16 if the window class is registered with <code>RegisterClassW()</code>, and therefore should "just work" regardless of location. However, <code>WM_CHAR</code> is generated by <code>TranslateMessage()</code>, and <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/ms644955(v=vs.85).aspx" rel="nofollow noreferrer">its documentation</a> says there is no way to determine whether a <code>WM_CHAR</code> has been generated or not, since <code>TranslateMessage()</code> always returns nonzero. I need to be able to determine whether the current keyboard message will be handled by the text system (and thus should be ignored); this is especially true since all non-text keys need to be handled in a layout-independent way (which I already have).</p>
  
  <p>I've also seen in the Windows 7 samples code for both the IMM API and the Text Services Framework. I'm not sure whether one is better than the other, and they both seem to do the same thing. Do they?</p>
  
  <p>In the case of IMM, there are a number of <code>WM_IMM_xxx</code> messages that I'm not sure if I should be ignoring or not, and every reference I've found seems to disagree as to whether I should handle them in a Unicode window or not... Also, the above problem of knowing if a given key event will be handled by IMM or not is still open; is there a way?</p>
  
  <p>TSF has a concept called ACP, which seems to allow me to use whatever text storage format I want for storing the text I'm actually going to use (that is, not an in-progress composition). Is this true? I'd like to be able to store my text as UTF-8 with attributes, converting to UTF-16 when drawing (for DirectWrite). Do the other API choices also let me do this?</p>
  
  <p>Or am I completely on the wrong path?</p>
  
  <p>And how would I <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/jj126268(v=vs.85).aspx" rel="nofollow noreferrer">opt</a> <a href="https://blogs.msdn.microsoft.com/oldnewthing/20150601-00/?p=45481" rel="nofollow noreferrer">into</a> <a href="https://blogs.msdn.microsoft.com/oldnewthing/20150608-00/?p=45431" rel="nofollow noreferrer">the</a> <a href="https://stackoverflow.com/questions/38774139/show-touch-keyboard-tabtip-exe-in-windows-10-anniversary-edition#comment64922814_38774139">on-screen keyboard</a> once I do all of that?</p>
  
  <p>Other references I used:</p>
  
  <ul>
  <li><a href="http://www.catch22.net/tuts/unicode-text-editing" rel="nofollow noreferrer">http://www.catch22.net/tuts/unicode-text-editing</a></li>
  </ul>
  
  <p>Thanks.</p>
  
  <p><strong>UPDATE</strong> 7 Nov 2016<br>After looking at the TsfPad sample again, I notice it also seems to just use <code>WM_CHAR</code>; now I'm not sure how it even uses TSF, other than that it does...</p>