S
WinForms and keyboard, a rather fun combination, especially when you try to make them work in concert.1. Developments Control.Key*Any controller can respond to the clavicle pressure and has identified several events: https://msdn.microsoft.com/ru-ru/library/system.windows.forms.control.keydown(v=vs.110).aspx ♪ https://msdn.microsoft.com/ru-ru/library/system.windows.forms.control.keypress(v=vs.110).aspx ♪ https://msdn.microsoft.com/ru-ru/library/system.windows.forms.control.keyup(v=vs.110).aspx ♪ I think it's okay to sign up for events and get a reaction to the keyboards. But there's one catch-- These events only work when the control is in♪ In addition, many controllers already have standard clavice processors, many respond to gaps as clicks, and TextBox and his relatives intercept almost all the keyboards.2. The shape is also counter.Class Form He also inherits from the counter, and he knows how to get these events. I think it's obvious we're moving the clavicle into shape and we're happy, it's kind of always in focus. But it's not that simple. If the daughters are in control of the clavicle and are in the focus (and if there's a daughter-in-law on the shape, one of them is necessarily in focus), then the shape simply ignores the claws.You can remember the nature of the form. https://msdn.microsoft.com/ru-ru/library/system.windows.forms.form.keypreview(v=vs.110).aspx which is intended to intercept events, then the events of the keyboard will first be transferred to form. It's kind of a working option, but if you don't want the standard counterfeit operation still working, you're gonna have to forget the properties. KeyPressEventArgs.Handled = false; If the event was processed at the shape level, it was transmitted to the subsidiary. To be honest, this is not the best option, because it has its limitations and not all the tasks can be accomplished in this way. Though he's kind of thinking about it.3. The menu will save us.First option to solve the problem without shamanship - add a menu in the face https://msdn.microsoft.com/ru-ru/library/system.windows.forms.menustrip(v=vs.110).aspx ♪ The menu is so designed that each of its elements can be tied to blocs and, for their correct operation, the menu intercepts all the events of the keyboard and then only those that do not involve pressurization and combinations. It's a good way for everyone, except for a couple of moments: for all the necessary keys and combinations, we need a menu and a menu in shape. If the first problem can still be solved by the use of hidden elements (not personally checked), then the second is a real problem, because not every form of application allows menu on it, but adding it only for the processing of the clave... you understand.4. Universal decisionTake your time to be happy, all the previous paragraphs required only the master of the mice click in the designer interface, and the code would be required.In shape, like any control, there's a virtual method. https://msdn.microsoft.com/ru-ru/library/system.windows.forms.control.processcmdkey(v=vs.110).aspx which is responsible for the default processing of the events of the keyboard, this is what needs to be redesigned in our form in order to obtain expected and correct work with the keyboard.protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
//ваша логика тут
return base.ProcessCmdKey(ref msg, keyData);
}
The basic method is not mandatory. The transfer of the event to the subsidiaries corresponds to the outcome of the method, only the logic is somewhat reversed: the true - the key is processed, no further processing is required; the false - event will be transmitted to the daughters of the controllers, even if you have done some pre-treatment. So you have complete control over the processing of the events of the form or caste control.The advantage of this method is that you have access to the parameter. keyDatawhich, unlike KeyPressEventArgs contains not only the symbol displayed, but all information about the pressurized key including scan-code, the condition of the keyboards and other useful information. What exactly happened is the parameter. msgwhich contains a WinAPI system message. We're interested in the keyboards. https://msdn.microsoft.com/ru-ru/library/windows/desktop/ms646280(v=vs.85).aspx and https://msdn.microsoft.com/ru-ru/library/windows/desktop/ms646281(v=vs.85).aspx ♪ KeyPress event is the actual consistent receipt of WM_KEYDOWN and WM_KEYUP.5. How do you use that?As usual. The clavicle and click on the button is the action of the user, which must trigger some reaction of the programme. The response is described by a separate privatized method, and in the processors, the button click and the clavicle are challenged. Such an approach avoids the need to copy the code, reduces the incident processing code to a minimum, allows the response of the programme to be given in any appropriate way, whether a counterfeit click, a checkbox or a radio battery, a keyboard or combination, a menu or all. The reaction code is in one place, and if you need to change it, you won't have to think where else you left it.If there is enough enthusiasm, I will later compile references to questions of good answers. In the meantime, the first thing we've found on the subject without a deep copy: https://ru.stackoverflow.com/q/554844/198316 https://ru.stackoverflow.com/q/433606/198316