How can I reposition my window so it isn’t covered by the touch keyboard?

Raymond Chen

Last week, we saw how we could rearrange our window contents to avoid having the touch keyboard cover the edit control. But what if the touch keyboard covers the entire window? No amount of rearranging will help. We’ll have to move our window.

Let’s make these changes to the Do­Layout function:

DoLayout(HWND hwnd, int cx, int cy, bool isKeyboardShowing = false)
  if (g_hwndChild) {
      if (IntersectRect(&rc, &rcEdit, &rcKeyboardClient)) {
        if ( > 50) {
          cyEdit = min(, cy);
        } else if (isKeyboardShowing) {
          // need to reposition the entire window, ugh.
          int dyAdjust = 50 -;
          RECT rcWindow;
          GetWindowRect(hwnd, &rcWindow);
          SetWindowPos(hwnd, nullptr,
            rcWindow.left, - dyAdjust, 0, 0,

If there are at least 50 pixels of the edit control visible, then we consider that good enough. If not, and if we are responding to the keyboard showing, then we take matters into our own hands and move our window so that there are 50 pixels of the edit control visible. I didn’t bother adding a check to make sure we never moved beyond the top of the work area; I’ll leave that as an exercise for the reader, seeing as it’s more typing that tends to distract from the point of the article.


Discussion is closed.

Feedback usabilla icon