1

I'm trying to make a simple text editor with winapi, its working for simple letter, but its not with capital letter or shift key.

char keys[256]; int x = 0; while (1) { for (x = 0; x <= 256; x++) { if (GetAsyncKeyState(x) == -32767) { char c[5]; GetKeyboardState(keys); ToAscii(x, MapVirtualKey(x, 0), keys, c, 0); putchar(c[0]); } } } 

3 Answers 3

1

The behaviors of keyboard input are far more complex than what you think. Your method doesn't work because:

  1. GetAsyncKeyState can miss keys. What happens when the user hits a key between calls?
  2. What happens when you hold down a key? What about keyboard repeat?
  3. Most critically, your code assumes a 1:1 relationship between key and character. You don't have any mechanism to deal with combinations like shift / caps lock state, or dead keys.

It would be better if you tried to explain what you're trying to do, and you can get advice on the right way to approach it. Trying to re-invent the behaviors of such a fundamental input device is not likely to be the best approach.

Sign up to request clarification or add additional context in comments.

2 Comments

I'm trying to make a text editor like notepad. But instead of using ToAscii I could make my own interpretation like VK_SHIFT + 0x41 make an 'A'... but i'll differ from azerty and qwerty right?
You should use a message loop and handle WM_CHAR if you are writing your own editor control. TranslateMessage generates this message based on keyboard activity; you should not try to re-invent this wheel.
1

GetAsyncKeyState is likely not the way to go here: the real way to make a text editor type control is to instead handle the WM_KEYDOWN and WM_CHAR messages. Windows will send these to your WndProc when your HWND has focus. This is the technique used by the Windows EDIT and RichEdit controls.

Use WM_KEYDOWN to handle the non-character keys - like arrows (VK_LEFT, VK_RIGHT), page up and so on; and use WM_CHAR for text characters. WM_KEYDOWN tells you the key pressed using a VK_ value, and doesn't take shift state into account; while WM_CHAR does take shift state, so gives you 'A' vs 'a' or '1' vs '!' as appropriate. (Note that you have to have TranslateMessage in your messsage loop for this to happen.)

Having said all that, an even easier/better thing to do is just use the existing Windows EDIT or RichEdit controls and let them do the work for you - there's rarely a good reason to reinvent the wheel - unless you're playing around for fun and learning Win32 perhaps. Writing a proper text editor is pretty complex; there's a lot of non-obvious stuff to consider, especially when you get into non-English text: you'd need to ensure it works correctly with right-to-left text (Arabic, Hebrew), works with IMEs which are used to enter Japanese and Chinese characters. And you have to ensure that your control is accessible to screenreaders so that users with visual impairments can still use the control. EDIT and RichEdit do all this for you.

The actual Notepad app, for example, is just a wrapper for an EDIT control; while WordPad just wraps a RichEdit control; both let the control do all the hard work, and just add the extra UI and file save/load functionality on top.

Comments

0

Try to call

GetKeyState(VK_CAPITAL); 

before

GetKeyboardState(keys); 

1 Comment

It's explained here

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.