Sign in to follow this  
LaudaDAgnus

[.net] System sound in Windows Forms (C++, Direct3D 9)

Recommended Posts

[size="2"]Hi everybody![/size]
[size="2"]Sorry my english, I'm russian developer.[/size]

[size="2"]Recently I started working with Windows Forms (Visual C++ 2010 Express) to create some utilities for my project. So I create form, place all controls I need and render my scene (Direct3D 9) into "Panel" object. Then I bound my render function with "Timer".[/size]

[size="2"]Now I trying to make WSAD-movement for the cam and here I encoutered with this proplem. Camera works nice, but it is the assidentally sound at the keydown event. Its sounds like MessageBox sound and I have not any ideas why and when (in code) this sound happends.[/size]

[size="2"][url="http://z-prog.info/download/editor02.rar%20"]http://z-prog.info/download/editor02.rar [/url]
[/size][size="2"](.NET Framework 4.0 and d3dx9_43.dll needed)[/size]

[size="2"]I working with treeView1_KeyDown, because it is in focus, when programm started.[/size]
[size="2"]Some problems with code sample pasting (size="2" in every line [img]http://public.gamedev.net/public/style_emoticons/default/mellow.gif[/img])[/size]
[size="2"]Here is code[/size]
[size="2"][url="http://www.everfall.com/paste/id.php?9woyzwrubfcy"]http://www.everfall....hp?9woyzwrubfcy[/url]

[/size]

[size="2"]Any ideas? Anybody faced with similar problem?[/size]

Share this post


Link to post
Share on other sites
The TreeView control does not know how to handle WASD and so makes the default beep when it receives a key that it doesn't support. One way to work around this would be to set [i]e.SuppressKeyPress = true;[/i] in your key event handler for each key you handle yourself (do not set it to true for keys you do not handle else those keys will stop working in the TreeView control).

A better way to handle key input may be to put the form in control. Ensure that the form gets a chance to receive keyboard events before the other controls claim them by setting the form's KeyPreview property to true. You can then handle the key events with the form's event handlers, rather than relying on a specific control (such as the TreeView control) being selected.

Share this post


Link to post
Share on other sites
thanks! no more sound with [color="#1C2837"][size="2"][i]e.SuppressKeyPress = true; on each key.[/i][/size][/color]
[color="#1C2837"][size="2"][i]
[/i][/size][/color]
[color="#1C2837"][size="2"][i]this is norm?[/i][/size][/color]
[color="#1C2837"][size="2"][i]
[/i][/size][/color]
[color="#1C2837"][size="2"][i][code]private: System::Void treeView1_KeyDown(System::Object^ sender, System::Windows::Forms::KeyEventArgs^ e)
{
switch(e->KeyCode)
{
case Keys::W :
e->SuppressKeyPress = true;
Scene.Editor.wp = true;
break;
case Keys::S :
e->SuppressKeyPress = true;
Scene.Editor.sp = true;
break;
case Keys::A :
e->SuppressKeyPress = true;
Scene.Editor.ap = true;
break;
case Keys::D :
e->SuppressKeyPress = true;
Scene.Editor.dp = true;
break;
}
}
private: System::Void treeView1_KeyUp(System::Object^ sender, System::Windows::Forms::KeyEventArgs^ e)
{
switch(e->KeyCode)
{
case Keys::W :
Scene.Editor.wp = false;
break;
case Keys::S :
Scene.Editor.sp = false;
break;
case Keys::A :
Scene.Editor.ap = false;
break;
case Keys::D :
Scene.Editor.dp = false;
break;
}
}[/code]
[/i][/size][/color]

Share this post


Link to post
Share on other sites
If it works, I guess so. It does still seem like a bit of a hack to put the keyboard handling code for the form into a TreeView control, though!

Share this post


Link to post
Share on other sites
[quote][color=#1C2837][size=2]A better way to handle key input may be to put the form in control.[/size][/color][/quote]

I don't understand this... How can I do this?

May be just call Form::Keydown method in keydown event of TreeView (and in other controls, that can be active)? It's will be norm?

Share this post


Link to post
Share on other sites
If you set the Form's KeyPreview property to "true" it will receive KeyUp/KeyDown events [i]before[/i] the control that has focus.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this