Hello,

I've been trying to get into game programming. So I learnt a simple 2d api, Direct2D because it has great documentation on msdn and started planning/programming my first game: a tetris clone.

All went well, and I also added sound and such, but now I want to add a high score table.
For that, the player needs to fill in his/her name. Now how do I do that?

The only text input i've known was the cin of console programs.
I want to make a input form like in html the <input type="text" />

So how do I do this? does winapi have some built in functionality to do this and how do most games do this? Do they perhaps create their own way for input?

Could you give me some clues? (doesn't have to be source code at all)

Thanks in advance,
Tigran

Dani AI

Generated

Nice progress — ’s WM_CHAR approach is exactly the lightweight solution expected for a short high-score name. Below are practical, non-redundant notes and options that address gaps people often hit when they move from a simple buffer to something more robust.

Two pragmatic choices

  • Host a real Windows Edit control briefly (CreateWindowEx with class "EDIT") on top of the Direct2D surface. It gives caret, selection, clipboard, IME and accessibility for free; read the text and destroy/hide the control when done. Best when OS behavior is acceptable.
  • Implement a custom text widget when visual style or integration requires it. Use WM_CHAR for character input and WM_KEYDOWN for navigation/deletion, but add IME support (WMIME* or TSF) for non‑Latin input. Use DirectWrite to measure and render the string and to compute caret positions.

Key implementation tips (custom widget)

  • Use IDWriteTextLayout::HitTestTextPosition to map a text index to an (x,y) caret coordinate, then draw the caret yourself (a 1–2 px vertical line). Example:

    FLOAT x, y;
    DWRITE_HIT_TEST_METRICS metrics;
    textLayout->HitTestTextPosition(caretIndex, FALSE, &x, &y, &metrics);
    // draw caret at (x, y) down to (x, y + metrics.height)
  • Handle surrogate pairs / UTF-16 indices when counting characters. Keep internal storage as a wide string (UTF-16) and map caret indices to code units or code points consciously.

  • Implement clipboard paste via GetClipboardData(CF_UNICODETEXT) and filter control characters and length.

  • Prefer drawing the caret manually (using the game render loop) over CreateCaret when using Direct2D, so the caret won’t be overwritten by frame clears.

  • Avoid DirectInput for text entry: it provides raw key codes and skips Windows character translation and IME.

Troubleshooting checklist

  • Confirm the window has focus and TranslateMessage is called.
  • If IME input is required, test with real IME (Chinese/Japanese) and consider TSF or the IMM APIs.
  • For single-line high-score names, an Edit overlay is the fastest, least error-prone choice.

(References in this thread: ’s WM_CHAR solution and ’s DirectInput suggestion — DirectInput is great for gameplay controls but not for translated/IME text entry.)

Hi, I'm not sure if you've solved this problem or not but getting keyboard input in a Windows Forms application is very straightforward, the event handlers are right there on the form itself. Maybe you can give some more detail about which part exactly you're finding difficult? What I do in my own games is use DirectInput so I strongly suggest checking out the samples for that API, it makes it easier to differentiate between immediate (controlling the player) input and buffered (entering your name) input. Hope this helps!

Thanks for your answer, but I already fixed it (should've posted the soluction, but forgot)
I used a TCHAR buffer with an int as caret position and added a letter every time I got a WM_CHAR message.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.