Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does CreateAcceleratorTable() not work without FVIRTKEY?

Tags:

c

winapi

I want to create an accelerator for CTRL+A. I noticed that it doesn't work when you leave out FVIRTKEY, i.e. the following code snippet doesn't work:

a.fVirt = FCONTROL;
a.key = 'A';
a.cmd = IDM_ADD;

(just for completeness' sake: using 'a' instead of 'A' doesn't work either)

This, however, works fine:

a.fVirt = FCONTROL|FVIRTKEY;
a.key = 'A';
a.cmd = IDM_ADD;

Can somebody explain this behaviour? MSDN says that if FVIRTKEY isn't set, "key" is interpreted as a character code which I presume to be ASCII. But it doesn't work which leaves me somewhat puzzled.

Thanks!

like image 312
Andreas Avatar asked Apr 01 '26 20:04

Andreas


1 Answers

The system translates certain Ctrl key combinations into ASCII control codes. The combination Ctrl+A is translated to the ASCII ctrl-A (SOH) character (ASCII value 0x01). This is the reason why your first snippet does not expose the desired behavior: It requires input that is impossible to enter. This is documented under Keyboard Input in the MSDN.

Also note that not providing the FVIRTKEY flag makes the specified key case-sensitive, i.e. 'a' and 'A' map to different input. It is usually desirable to use virtual key codes, to provide a consistent keyboard UI. The implications are explained under About Keyboard Accelerators - Accelerator Keystroke Assignments.

like image 131
IInspectable Avatar answered Apr 04 '26 14:04

IInspectable



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!