none
Windows.Forms.Keys to DirectX.DirectInput.Key

    Question

  • In my application I am getting a keycode integer 119 for Windows.Forms.Keys.F8 from a KeyDown event for a textbox which I then need to use as an argument to a function which requires DirectX.DirectInput.Key.F8 which is integer 66.

    I have been Googling for hours and can't find anything better than creating a huge Select Case statement mapping every key in both enumerations. Is there a better way?
    Tuesday, August 12, 2008 1:17 PM

Answers

All replies

  •  Unless there is some "math" involved...that was my first thought.  I.E. If everytime you subtract (119 - 66) 53 from the ASCII code.  I suspect it's only a range of translations...not everything, so you may get away with only translating certain values and leaving the rest.

    Good Luck
    Tuesday, August 12, 2008 1:36 PM
  • Please check whether this will help.

    http://gpwiki.org/index.php/DirectX:DirectInput:Tutorials:VBNET:DX9:Keyboard_Handling_Immediate 


    Or else you might want to define a list / map which maps windows keycode and directX keycode. With that you might be able to get away with the select / if statements.
    Tuesday, August 12, 2008 1:38 PM
  • AproposOyVey said:

     Unless there is some "math" involved...that was my first thought.  I.E. If everytime you subtract (119 - 66) 53 from the ASCII code.  I suspect it's only a range of translations...not everything, so you may get away with only translating certain values and leaving the rest.

    Good Luck


    Unfortunately there is no correlation I can see between the two enums.

    What if I made my own enum that mapped the two keys together? I am new to VB how would I do this?

    Here is my code so far.

    Private Sub TextBox2_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox2.KeyDown
    If e.Modifiers.ToString() <> "None" Then
    strHotKey = e.Modifiers.ToString() & "+" & e.KeyCode.ToString()
    Else
    strHotKey = e.KeyCode.ToString()
    End If

    Select Case strHotKey
    Case "Alt+F4"
    MsgBox(strHotKey & " is not avalable", MsgBoxStyle.Information)
    TextBox2.Text = "unavailable"
    Case Else
    TextBox2.Text = strHotKey
    ' _keyboard.Poll()

    My.Settings.valStartHotkey = e.KeyCode
    My.Settings.strStartHotKey = strHotKey
    My.Settings.Save()
    End Select
    End Sub

    So this is the first bit the user enters the textbox, hits a key or key combination and I save that key in My.Settings

    Below is the Sub fired by a timer to look for key presses. I have to replace Key.F8 and Key.F7 with the user defined keys, stored in My.Settings, but they are Windows.forms.Keys enum which give the wrong integer. I hope i have explained this clearly enough.

    Private Sub CheckForInput()
    _keyboard.Poll()
    ' _mouse.Poll()
    'If _keyboard.State(Key.Escape) Then
    ' Timer1.Enabled = False
    'End If
    If _keyboard.State(Key.F8 Then
    MoveMouse()
    'MsgBox(toxy(3, 1))
    End If
    If _keyboard.State(Key.F7) Then
    mystop = 1
    End If
    'If Not (0 = _mouse.MouseButtons(1)) Then
    ' MsgBox("Fire!")
    'End If
    End Sub

    Module Keyboard

    Imports Microsoft.DirectX.DirectInput

    Class Keyboard
    Implements IDisposable

    Public Sub New(ByVal form As Form)
    _device = New Device(SystemGuid.Keyboard)
    _device.SetCooperativeLevel(form, CooperativeLevelFlags.Background Or CooperativeLevelFlags.NonExclusive)
    _device.SetDataFormat(DeviceDataFormat.Keyboard)
    Try
    _device.Acquire()
    Catch ex As Microsoft.DirectX.DirectXException
    MsgBox(ex.Message, 64, "Critical error")
    End Try
    End Sub

    Public Sub Poll()
    Try
    _device.Poll()
    _state = _device.GetCurrentKeyboardState
    Catch generatedExceptionVariable0 As NotAcquiredException
    Try
    _device.Acquire()
    Catch iex As InputException
    MsgBox(iex.Message, 64, "Critical error")
    End Try
    Catch ex2 As InputException
    MsgBox(ex2.Message, 64, "Critical error")
    End Try
    End Sub

    Public ReadOnly Property State() As KeyboardState
    Get
    Return _state
    End Get
    End Property

    Public Sub Dispose() Implements IDisposable.Dispose
    Dispose(True)
    GC.SuppressFinalize(Me)
    End Sub

    Overridable Sub Dispose(ByVal disposing As Boolean)
    If Not Me._disposed Then
    If disposing Then
    If Not (_device Is Nothing) Then
    _device.Unacquire()
    End If
    End If
    End If
    _disposed = True
    End Sub


    Private _disposed As Boolean
    Private _device As Device
    Private _state As KeyboardState
    End Class
    Wednesday, August 13, 2008 2:17 AM