locked
D3DImage and DirectInput keyboard focus problem in wpf application RRS feed

  • Question

  • Hi Everyone

    Due to performance problems with my wpf 3d application I have recently tried to implement the graphics functionality using DirectX9 (with DirectInput) and the WPF 3.5sp1 D3DImage component.  Using directInput8 in my dll, I am able to turn my D3DImage using the left and right arrow keys.

    My problem is that if i add e.g. a textbox to the wpf tree and use the arrow keys in the textbox to edit text in the textbox the D3Dimage keeps turning which ín my opinion really isnt the best solution. 

    What is the best way to get around the problem of my D3Dimage moving while I am using the arrow keys in the textbox.

    Below is the XAML for my WPF window
        <DockPanel LastChildFill="True" Name="dockPanel1">
            <StackPanel DockPanel.Dock="Right" Orientation="Vertical" Name="stackPanel1">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="FOV" />
                    <TextBox Name="txtBxFieldOfView"  Width="100"/>
                </StackPanel>
            </StackPanel>
                <Grid Name="grid1">
                    <Grid.Background>
                        <ImageBrush>
                            <ImageBrush.ImageSource>
                                <i:D3DImage x:Name="d3dimg" />
                            </ImageBrush.ImageSource>
                        </ImageBrush>
                    </Grid.Background>
                </Grid>
        </DockPanel>
    


    My code behind for initializing the Directinput functions is as follows:
    private void RootWindow_Loaded(object sender, RoutedEventArgs e) {
    
                // Get the hande of our main window and initialize DirectInput in DLL
                HwndSource source = HwndSource.FromHwnd(new WindowInteropHelper(this).Handle);
                // source.AddHook(new HwndSourceHook(WndProc));
               IntPtr hWnd = source.Handle;
    
                InitializeInput(hWnd);
            }
    


    In my DLL I initialize DirectInput with
    void initDInput(HWND hWnd)
    {
    
    	HINSTANCE hInstance = GetModuleHandle(NULL);
    	HRESULT hr;
        // create the DirectInput interface
        hr = DirectInput8Create(hInstance,    // the handle to the dll
                           DIRECTINPUT_VERSION,    // the compatible version
                           IID_IDirectInput8,    // the DirectInput interface version
                           (void**)&din,    // the pointer to the interface
                           NULL);             
    
        // create the keyboard device
        hr = din->CreateDevice(GUID_SysKeyboard,    // the default keyboard ID being used
                          &dinkeyboard,    // the pointer to the device interface
                          NULL);    
    
        // set the data format to keyboard format
        hr = dinkeyboard->SetDataFormat(&c_dfDIKeyboard);
    
        // set the control we will have over the keyboard
        hr = dinkeyboard->SetCooperativeLevel(hWnd, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND);
    
    }
    


    In my c++ DirectX render function I detect my keyboard input using detect_input as follows
    // Called in Render function ( I have also tried placing it in the messageProc )
    void detect_input(void)
    {
     	HRESULT hr;
        // get access if we don't have it already
        if (dinkeyboard == NULL)
    	return;
        hr = dinkeyboard->Acquire();
        // get the input data
        hr = dinkeyboard->GetDeviceState(256, (LPVOID)keystate);
    }
    



    I Have tried the following.

    (a) Hook into the windows message handler using AddHook and process keyboard events only for the element with focus -> pretty cumbersome solution
    (b) Place my D3Dimage in a WindowsFormsHost and use the WindowsFormsHost.Handle to initialize my DirectInput -> no effect
    (c) Using handle of Window initialized in DLL -> Keyboard input does not get routed to the DirectInput functions in the dll.


    What is the best way to get around the problem of my D3Dimage moving while I am using the arrow keys in the textbox ?

    Help will really be appreciated.

    Regards Hans
    • Moved by Linda Liu Thursday, February 11, 2010 9:44 AM related to DirectX (From:Windows Presentation Foundation (WPF))
    Monday, February 8, 2010 12:03 PM

Answers

  • I have decided to abandon the idea of using directinput.
    The option of using the standard mouse input from WPF seems more usefull. Using a transparent border over the D3DImage and then grabbing mouse input from the wpf border seems to be the best way to do it.
    • Marked as answer by Hans Potgieter Wednesday, February 24, 2010 8:32 PM
    Wednesday, February 24, 2010 8:32 PM

All replies

  • Hello,

     

    Thank you for your post!  I would suggest creating a new thread for your question in the DirectX forum located here:  (http://forums.xna.com/forums/).

    Hope that would be helpful.

    Have a great day!

    Thanks & regards,


    Ashawani Tier 2 Application Support Server and Tools Online Engineering Live Services Team
    • Proposed as answer by ashawani_dubey Thursday, February 18, 2010 10:08 PM
    Thursday, February 18, 2010 10:08 PM
  • I have decided to abandon the idea of using directinput.
    The option of using the standard mouse input from WPF seems more usefull. Using a transparent border over the D3DImage and then grabbing mouse input from the wpf border seems to be the best way to do it.
    • Marked as answer by Hans Potgieter Wednesday, February 24, 2010 8:32 PM
    Wednesday, February 24, 2010 8:32 PM