KeyPress Question

  • Thread starter Thread starter meh
  • Start date Start date
M

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);
}

}
 
meh said:
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);
}
 
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.
 
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) said:
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 said:
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);
}

}
 
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 said:
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) said:
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 said:
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);
}

}
 
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) said:
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 said:
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) said:
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]


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);
}

}
 
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 said:
Stoitcho Goutsev (100) said:
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 said:
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);
}

}

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]


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);
}

}
 
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
 
Back
Top