Click here to Skip to main content
16,016,345 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all.i have done a plug in for rhinoceros.now my problem is that in one of my forms i would like to give a different value to some variables by adding a double while clicking "a" or "s" on the keyboard.i tried using the events of my form like keypress and and the event of rhinoceros for keyboardkeypress but i can't understand why it is not always working.sometimes it runs sometimes it does not.i tried to call it again if the form lost focus or gotfocus but no result.then another problem is that by using keyboard it writs on rhinocommand line,and i don't want,but i don't know any function to avoid it.any suggestion?i tried globalKeyboardHook but it gives me a callback error.
now i don't know if i have to use the keypress event for all controls in my forms or other.i hope not to be too mysterious.thanks.here there is an example of wath i have done.i thought it would be easy...

dblvariable=0;

private void myform_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar.ToString() == "a")
{
dblvariable=dblvariable+0.05;
}
}
Posted
Comments
Paddy84 27-Feb-14 11:54am    
I'm not sure of a solution but I know that the myform_KeyPress event will only happen when the actual form has focus, and not if a control has focus.
BillWoodruff 27-Feb-14 12:52pm    
I would guess that many here are not familiar with Rhinoceros, let alone its plug-in architecture. So, I think that if you want help, you'll have to get much more specific.

Have you set your Form's 'KeyPreview property to 'true ?
gianfrancoguzzo 27-Feb-14 13:02pm    
i was looking at keypreview property and it is set to false,probably it helps,i don't know this property,i am googeling now and i m tryiing if it works.thank you
BillWoodruff 27-Feb-14 13:47pm    
Even with a Form's 'KeyPreview property set to 'true, the Form must have focus in order to receive key-press-down-up Events.

Detecting key Events when the Form does not have focus will require use of a Global Hook.
Sergey Alexandrovich Kryukov 27-Feb-14 14:05pm    
Keys are not characters!
—SA

1 solution

Maybe you should go for Control.KeyDown[^] event which occurs when a key is pressed while the control has focus.

For example,
C#
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.A)
    {
        MessageBox.Show("A pressed.");
    }

    if (e.KeyCode == Keys.S)
    {
        MessageBox.Show("S pressed.");
    }
}


-KR
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 27-Feb-14 14:05pm    
Right. I would also show how an event handler is added, as many inquirers mix up things and have problems with that. I voted 4 this time.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900