java - C# WinForms Key Bindings -
what closest equivalent of java's keybindings in c#? attempting port swing application c#, unclear method should use. here sample of java porting:
action goup = new abstractaction() { @override public void actionperformed(actionevent e) { panel.uppressed = true; } }; action stopup = new abstractaction() { @override public void actionperformed(actionevent e) { panel.uppressed = false; } }; inputmap getinputmap = panel.getinputmap(jcomponent.when_in_focused_window); getinputmap.put(keystroke.getkeystroke("w"), "goup"); getinputmap.put(keystroke.getkeystroke("released w"), "stopup"); panel.getactionmap().put("goup", goup); panel.getactionmap().put("stopup", stopup);
in winforms, not aware of inputmap
. keyboard input handled in 1 of 2 ways:
- shortcut keys, i.e.
menuitem.shortcut
. assignable in designer, or in code. pressing associated key invoke menu item'sclick
event handler(s). of course, use method requires action want take represented in ui menu item. - various key event handling in ui. can complicated depending on needs, appropriate way, , primary way handle key input not associated menu item. simplest event
keypress
event. given example, want handlekeydown
,keyup
can track actual state of key. note receive multiplekeydown
events auto-repeat kicks in key; if problem handler, you'll need track key state , key you've seenkeydown
, handlekeydown
event if you've seen correspondingkeyup
event.
note wpf have key binding model more similar inputmap
. of course, syntax entirely different, breaks apart idea of menu items, commands, , key bindings, can mix , match necessary (and in particular, have key bindings don't correspond menu item). if using wpf option, might find part of transition easier (and part, since swing otherwise more winforms wpf :) ).
Comments
Post a Comment