By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,978 Members | 1,093 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,978 IT Pros & Developers. It's quick & easy.

KeyPress Question

P: n/a
meh
This should wotk I think but its not trapping the Insert or Delete key???
Do I not have this written correctly?
if (e.KeyCode == Keys.Insert)
{
MessageBox.Show("Pressed " + e.KeyCode);
if (e.Control)
{
MessageBox.Show("Pressed " + e.KeyCode);
}
else if (e.Shift)
{
MessageBox.Show("Pressed " + e.KeyCode);
}
else
{
MessageBox.Show("Pressed " + e.KeyCode);
}

}
Nov 16 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
meh wrote:
This should wotk I think but its not trapping the Insert or Delete key???
Do I not have this written correctly?

if (e.KeyCode == Keys.Insert)
{
MessageBox.Show("Pressed " + e.KeyCode);
if (e.Control)
{
MessageBox.Show("Pressed " + e.KeyCode);
}
else if (e.Shift)
{
MessageBox.Show("Pressed " + e.KeyCode);
}
else
{
MessageBox.Show("Pressed " + e.KeyCode);
}

}


Isn't the following what you're after?

if (e.KeyCode == Keys.Insert)
{
MessageBox.Show("Pressed " + e.KeyCode);
}
else if (e.Control)
{
MessageBox.Show("Pressed " + e.KeyCode);
}
else if (e.Shift)
{
MessageBox.Show("Pressed " + e.KeyCode);
}
else
{
MessageBox.Show("Pressed " + e.KeyCode);
}
Nov 16 '05 #2

P: n/a
Hi meh,

If you read carefully in MSDN there is written

MSDN:
"The KeyPress event is not raised by noncharacter keys; however, the
noncharacter keys do raise the KeyDown and KeyUp events."

Delete and Insert keys are non-character keys.
--
HTH
Stoitcho Goutsev (100) [C# MVP]
"meh" <no*************@cox.net> wrote in message
news:eg**************@TK2MSFTNGP10.phx.gbl...
This should wotk I think but its not trapping the Insert or Delete key???
Do I not have this written correctly?
if (e.KeyCode == Keys.Insert)
{
MessageBox.Show("Pressed " + e.KeyCode);
if (e.Control)
{
MessageBox.Show("Pressed " + e.KeyCode);
}
else if (e.Shift)
{
MessageBox.Show("Pressed " + e.KeyCode);
}
else
{
MessageBox.Show("Pressed " + e.KeyCode);
}

}

Nov 16 '05 #3

P: n/a
meh
Sorry I forgot...I am using the KeyDown event i.e.

private void treeView1_KeyDown(object sender,
System.Windows.Forms.KeyEventArgs e)
{

if (e.KeyCode == Keys.Insert)
{
MessageBox.Show("Pressed " + e.KeyCode);
if (e.Control)
{
MessageBox.Show("Pressed " + e.KeyCode);
}
else if (e.Shift)
{
MessageBox.Show("Pressed " + e.KeyCode);
}
else
{
MessageBox.Show("Pressed " + e.KeyCode);
}

}

"Stoitcho Goutsev (100) [C# MVP]" <10*@100.com> wrote in message
news:eh**************@TK2MSFTNGP09.phx.gbl...
Hi meh,

If you read carefully in MSDN there is written

MSDN:
"The KeyPress event is not raised by noncharacter keys; however, the
noncharacter keys do raise the KeyDown and KeyUp events."

Delete and Insert keys are non-character keys.
--
HTH
Stoitcho Goutsev (100) [C# MVP]
"meh" <no*************@cox.net> wrote in message
news:eg**************@TK2MSFTNGP10.phx.gbl...
This should wotk I think but its not trapping the Insert or Delete key???
Do I not have this written correctly?
if (e.KeyCode == Keys.Insert)
{
MessageBox.Show("Pressed " + e.KeyCode);
if (e.Control)
{
MessageBox.Show("Pressed " + e.KeyCode);
}
else if (e.Shift)
{
MessageBox.Show("Pressed " + e.KeyCode);
}
else
{
MessageBox.Show("Pressed " + e.KeyCode);
}

}


Nov 16 '05 #4

P: n/a
In this case it works with me.
If the problem is that you don't see a message box that says the Ctrl is
pressed that is because the Control is a modifier. Change the followng lines
if (e.Control)
{
MessageBox.Show("Pressed " + e.KeyCode);
}
to

if (e.Control)
{
MessageBox.Show("Pressed " + e.Modifiers);
}

and you'll see the message

--
HTH
Stoitcho Goutsev (100) [C# MVP]
"meh" <no*************@cox.net> wrote in message
news:uL*************@tk2msftngp13.phx.gbl... Sorry I forgot...I am using the KeyDown event i.e.

private void treeView1_KeyDown(object sender,
System.Windows.Forms.KeyEventArgs e)
{

if (e.KeyCode == Keys.Insert)
{
MessageBox.Show("Pressed " + e.KeyCode);
if (e.Control)
{
MessageBox.Show("Pressed " + e.KeyCode);
}
else if (e.Shift)
{
MessageBox.Show("Pressed " + e.KeyCode);
}
else
{
MessageBox.Show("Pressed " + e.KeyCode);
}

}

"Stoitcho Goutsev (100) [C# MVP]" <10*@100.com> wrote in message
news:eh**************@TK2MSFTNGP09.phx.gbl...
Hi meh,

If you read carefully in MSDN there is written

MSDN:
"The KeyPress event is not raised by noncharacter keys; however, the
noncharacter keys do raise the KeyDown and KeyUp events."

Delete and Insert keys are non-character keys.
--
HTH
Stoitcho Goutsev (100) [C# MVP]
"meh" <no*************@cox.net> wrote in message
news:eg**************@TK2MSFTNGP10.phx.gbl...
This should wotk I think but its not trapping the Insert or Delete key??? Do I not have this written correctly?
if (e.KeyCode == Keys.Insert)
{
MessageBox.Show("Pressed " + e.KeyCode);
if (e.Control)
{
MessageBox.Show("Pressed " + e.KeyCode);
}
else if (e.Shift)
{
MessageBox.Show("Pressed " + e.KeyCode);
}
else
{
MessageBox.Show("Pressed " + e.KeyCode);
}

}



Nov 16 '05 #5

P: n/a
meh
The Control key works but
I cant get it to trap the Insert key...

if (e.KeyCode == Keys.Insert)
{
MessageBox.Show("Pressed " + e.KeyCode);
{
"Stoitcho Goutsev (100) [C# MVP]" <10*@100.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
In this case it works with me.
If the problem is that you don't see a message box that says the Ctrl is
pressed that is because the Control is a modifier. Change the followng
lines
if (e.Control)
{
MessageBox.Show("Pressed " + e.KeyCode);
}


to

if (e.Control)
{
MessageBox.Show("Pressed " + e.Modifiers);
}

and you'll see the message

--
HTH
Stoitcho Goutsev (100) [C# MVP]
"meh" <no*************@cox.net> wrote in message
news:uL*************@tk2msftngp13.phx.gbl...
Sorry I forgot...I am using the KeyDown event i.e.

private void treeView1_KeyDown(object sender,
System.Windows.Forms.KeyEventArgs e)
{

if (e.KeyCode == Keys.Insert)
{
MessageBox.Show("Pressed " + e.KeyCode);
if (e.Control)
{
MessageBox.Show("Pressed " + e.KeyCode);
}
else if (e.Shift)
{
MessageBox.Show("Pressed " + e.KeyCode);
}
else
{
MessageBox.Show("Pressed " + e.KeyCode);
}

}

"Stoitcho Goutsev (100) [C# MVP]" <10*@100.com> wrote in message
news:eh**************@TK2MSFTNGP09.phx.gbl...
> Hi meh,
>
> If you read carefully in MSDN there is written
>
> MSDN:
> "The KeyPress event is not raised by noncharacter keys; however, the
> noncharacter keys do raise the KeyDown and KeyUp events."
>
> Delete and Insert keys are non-character keys.
> --
> HTH
> Stoitcho Goutsev (100) [C# MVP]
>
>
> "meh" <no*************@cox.net> wrote in message
> news:eg**************@TK2MSFTNGP10.phx.gbl...
>> This should wotk I think but its not trapping the Insert or Delete key??? >> Do I not have this written correctly?
>>
>>
>> if (e.KeyCode == Keys.Insert)
>> {
>> MessageBox.Show("Pressed " + e.KeyCode);
>> if (e.Control)
>> {
>> MessageBox.Show("Pressed " + e.KeyCode);
>> }
>> else if (e.Shift)
>> {
>> MessageBox.Show("Pressed " + e.KeyCode);
>> }
>> else
>> {
>> MessageBox.Show("Pressed " + e.KeyCode);
>> }
>>
>> }
>>
>>
>
>



Nov 16 '05 #6

P: n/a
> The Control key works but
I cant get it to trap the Insert key...

if (e.KeyCode == Keys.Insert)
{
MessageBox.Show("Pressed " + e.KeyCode);
}

What do you mean?
In the code you posted if you don't trap the insert key you won't see the
message for the Control modifier.

How come it works?
When I test your code I got the message for both Insert and Ctrl. make sure
you trap KeyDown not KeyPress event. Then make sure that the treeview has
the focus. In order to focus the treeview you have to have nodes inserted.
--

Stoitcho Goutsev (100) [C# MVP]
"meh" <no*************@cox.net> wrote in message
news:ub**************@TK2MSFTNGP09.phx.gbl...
"Stoitcho Goutsev (100) [C# MVP]" <10*@100.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
In this case it works with me.
If the problem is that you don't see a message box that says the Ctrl is
pressed that is because the Control is a modifier. Change the followng
lines
if (e.Control)
{
MessageBox.Show("Pressed " + e.KeyCode);
}


to

if (e.Control)
{
MessageBox.Show("Pressed " + e.Modifiers);
}

and you'll see the message

--
HTH
Stoitcho Goutsev (100) [C# MVP]
"meh" <no*************@cox.net> wrote in message
news:uL*************@tk2msftngp13.phx.gbl...
Sorry I forgot...I am using the KeyDown event i.e.

private void treeView1_KeyDown(object sender,
System.Windows.Forms.KeyEventArgs e)
{

if (e.KeyCode == Keys.Insert)
{
MessageBox.Show("Pressed " + e.KeyCode);
if (e.Control)
{
MessageBox.Show("Pressed " + e.KeyCode);
}
else if (e.Shift)
{
MessageBox.Show("Pressed " + e.KeyCode);
}
else
{
MessageBox.Show("Pressed " + e.KeyCode);
}

}

"Stoitcho Goutsev (100) [C# MVP]" <10*@100.com> wrote in message
news:eh**************@TK2MSFTNGP09.phx.gbl...
> Hi meh,
>
> If you read carefully in MSDN there is written
>
> MSDN:
> "The KeyPress event is not raised by noncharacter keys; however, the
> noncharacter keys do raise the KeyDown and KeyUp events."
>
> Delete and Insert keys are non-character keys.
> --
> HTH
> Stoitcho Goutsev (100) [C# MVP]
>
>
> "meh" <no*************@cox.net> wrote in message
> news:eg**************@TK2MSFTNGP10.phx.gbl...
>> This should wotk I think but its not trapping the Insert or Delete

key???
>> Do I not have this written correctly?
>>
>>
>> if (e.KeyCode == Keys.Insert)
>> {
>> MessageBox.Show("Pressed " + e.KeyCode);
>> if (e.Control)
>> {
>> MessageBox.Show("Pressed " + e.KeyCode);
>> }
>> else if (e.Shift)
>> {
>> MessageBox.Show("Pressed " + e.KeyCode);
>> }
>> else
>> {
>> MessageBox.Show("Pressed " + e.KeyCode);
>> }
>>
>> }
>>
>>
>
>



Nov 16 '05 #7

P: n/a
meh
As a follow up....5 hours later.....the reason the Insert and Delete key
were not being trapping was because I had a context menu assigned to the
treeview???

Go figure......

meh
"meh" <no*************@cox.net> wrote in message
news:eg**************@TK2MSFTNGP10.phx.gbl...
This should wotk I think but its not trapping the Insert or Delete key???
Do I not have this written correctly?
if (e.KeyCode == Keys.Insert)
{
MessageBox.Show("Pressed " + e.KeyCode);
if (e.Control)
{
MessageBox.Show("Pressed " + e.KeyCode);
}
else if (e.Shift)
{
MessageBox.Show("Pressed " + e.KeyCode);
}
else
{
MessageBox.Show("Pressed " + e.KeyCode);
}

}

Nov 16 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.