low-level keyboard using DirectInput

Discussion in 'C and C++' started by trismund, Jul 22, 2015.

  1. trismund

    trismund King in the East Staff Member Administrator

    Joined:
    Oct 28, 2012
    Messages:
    2,816
    Likes Received:
    306
    Trophy Points:
    83
    Location:
    Locating...


    Code:
    
    #include <windows.h>
    #include <stdio.h>
    #include <InitGuid.h>
    #define DIRECTINPUT_VERSION 0x0800
    #include <dinput.h>
    
    #pragma comment (lib, "dinput8.lib")
    
    LPDIRECTINPUT8 din;   
    LPDIRECTINPUTDEVICE8 directinputdevice;  
    
    void InitializeDirectInput(HINSTANCE hInstance, HWND hWnd)
    {
        DirectInput8Create(hInstance,   
            DIRECTINPUT_VERSION,    
            IID_IDirectInput8,    
            (void**)&din,    
            NULL);   
        din->CreateDevice(GUID_SysKeyboard,  
            &directinputdevice,   
            NULL);    
        directinputdevice->SetDataFormat(&c_dfDIKeyboard); 
        directinputdevice->SetCooperativeLevel(hWnd,
            DISCL_NONEXCLUSIVE | DISCL_BACKGROUND);
    }
    
    void DetectKeys(void)
    {
        static BYTE keystate[256];    
        directinputdevice->Acquire();   
        directinputdevice->GetDeviceState(256, (LPVOID)keystate);
        if(keystate[DIK_A] & 0x80)  // Check for a depressed 'A'   
            printf("A\n");          // This is where you'll implement
                                    // your custom key capture routine  
    }
    
    void CloseDirectInput(void)
    {
        directinputdevice->Unacquire(); 
        din->Release();    
    }
    
    void MessageLoop()
    {
        MSG msg;
        while(TRUE)
        {
            DWORD dwStartPoint = GetTickCount();
            if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
            {
                if (msg.message == WM_QUIT)
                    break;
                TranslateMessage(&msg);
                DispatchMessage(&msg);
            } 
            DetectKeys();
            while ((GetTickCount() - dwStartPoint) < 25);
        }
        CloseDirectInput();
    }
    
    DWORD WINAPI MyDirectInput(LPVOID lpParm)
    {
        HINSTANCE hInstance = GetModuleHandle(NULL);
        InitializeDirectInput(hInstance, NULL);
        MessageLoop();
        return 0;
    }
    
    int main(int argc, char** argv)
    {
        HANDLE hThread;
        DWORD dwThread;
    
        hThread = CreateThread(NULL,NULL,(LPTHREAD_START_ROUTINE)
            MyDirectInput, (LPVOID) argv[0], NULL, &dwThread);
        if (hThread)
            return WaitForSingleObject(hThread,INFINITE);
        else return 1;
    }
    
    


     
  2. Jhunz1337

    Jhunz1337 Newbie Established Member

    Joined:
    Jul 4, 2014
    Messages:
    334
    Likes Received:
    3
    Trophy Points:
    18
    Location:
    Planet X


    [mod]:good:Approved![/mod]

    pero kng ilang Features lng pedeng Auto On nlng hahahaha!~
     
  3. WarOfRen

    WarOfRen Leecher

    Joined:
    Aug 3, 2015
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0


    Haaaiizzzz!!!!

    Ngaung araw halos 24 hours ako nag search for DirectInput in VB.Net pero hindi talaga gumana..
    naisip ko mag post ako ng request dito :)
    tapos bigla kong nakita to!!!!
    Convert konalang salamat sir Tristmund!

    gagamitin ko ksi sa RanWorld pra sa auto keyboard :3

    Please login or register to view links
     
  4. WarOfRen

    WarOfRen Leecher

    Joined:
    Aug 3, 2015
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0


    Ugh...

    C++ pla d maconvert :/
    hehehe pwede pa hingi ng vb.net or C# code :)
     
  5. trismund

    trismund King in the East Staff Member Administrator

    Joined:
    Oct 28, 2012
    Messages:
    2,816
    Likes Received:
    306
    Trophy Points:
    83
    Location:
    Locating...


    Code:
    Imports Microsoft.DirectX Imports Microsoft.DirectX.DirectInput.CooperativeLevelFlags Imports Microsoft.DirectX.DirectInput
    
    
    Public Class Form1
        Inherits System.Windows.Forms.Form
       Dim KB As New DirectInput.Device(DirectInput.SystemGuid.Keyboard)
    
    
       Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
           KB.SetCooperativeLevel(Me, Background Or NonExclusive)
           Me.Show()
           KB.Acquire()
       End Sub
    
    
       Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
           KB.Dispose()
           KB = Nothing
       End Sub
    
    
       Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
           Dim State As DirectInput.KeyboardState
           State = KB.GetCurrentKeyboardState
           If State.Item(Key.Escape) Then Me.Close()
           If State.Item(Key.A) Then Label1.Text &= "a"
           If State.Item(Key.B) Then Label1.Text &= "b"
           If State.Item(Key.C) Then Label1.Text &= "c"
       End Sub
    
    End Class
    
     
  6. trismund

    trismund King in the East Staff Member Administrator

    Joined:
    Oct 28, 2012
    Messages:
    2,816
    Likes Received:
    306
    Trophy Points:
    83
    Location:
    Locating...


    Pwede yan kahit ilang features, depende sa code mo.
     
  7. WarOfRen

    WarOfRen Leecher

    Joined:
    Aug 3, 2015
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0


    Na try ko na to eh...
    kso hindi lumalabas ung Form1 ko na parang background lang sya or invisible :tehe:
    Visual Studio 2015 kasi gamit ko and DirectX 11 SDK.. dito kaya un? ang code na yan hindi gumagana dahil old version na yan :/
     
  8. WarOfRen

    WarOfRen Leecher

    Joined:
    Aug 3, 2015
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0


    Alam ko na itatanong ko....

    About *
    -Keyboard Hooking in VB.Net and
    -Sending Key in DirectX Game

    (I search for this that's why i get info about DirectInput)
     
  9. trismund

    trismund King in the East Staff Member Administrator

    Joined:
    Oct 28, 2012
    Messages:
    2,816
    Likes Received:
    306
    Trophy Points:
    83
    Location:
    Locating...


    getasynckeystate na try mo na?
     
  10. WarOfRen

    WarOfRen Leecher

    Joined:
    Aug 3, 2015
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0


    Hindi pa, try ko plang.. :cool:
     
  11. WarOfRen

    WarOfRen Leecher

    Joined:
    Aug 3, 2015
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0


    Sir trismund :confused:

    pwede ba download kayo nito : Please login or register to view links
    tapos kayo mag directinput :)

    hirap ksi eh.. getasynckeystate hindi working..
    sabi ng iba directinput daw.. bka daw dapat kpag directinput kung directx 9.0c daw sa game dapat yan din gamitin ko sa vb.net :)
     
  12. WarOfRen

    WarOfRen Leecher

    Joined:
    Aug 3, 2015
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0


    Sir Trismund!

    Ito nlang po request ko..

    AutoIt kasi pwede gamitin sa paggawa ng Auto Keyboard in DirectX Game..
    so... pwede po malaman or paturo tungkol dito..:

    Balak ko po kasi i attach ung AutoIt.exe file na sinasabi kong bot sa VB.net ko na ginagawa ko palang..
    at balak ko din po ksing gawing Invisible ung AutoIt.exe file na yan or gawing 0 Opacity.. kung alam nyo po pwede pa reply at kung hindi ignore nlng po :)
     
  13. WarOfRen

    WarOfRen Leecher

    Joined:
    Aug 3, 2015
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0


    [​IMG]
    Ito sir...

    system.runtime.remoting.dll
    Cannot find or open PDB files

    then after i launch the app manually in
    I get "WindowsApplication22.exe has stopped working."

    Any ideas?

    sir ung Low-level keyboard hook ba kpag ginamit ko pwede ko na gamitin ung Sendkey or Getasynckeystate?
     
    Last edited by a moderator: Aug 8, 2015
  14. trismund

    trismund King in the East Staff Member Administrator

    Joined:
    Oct 28, 2012
    Messages:
    2,816
    Likes Received:
    306
    Trophy Points:
    83
    Location:
    Locating...


    Susubukan ko pag may time. hindi na kasi ako masyado gumagamit ng Visual Basic
     
  15. steb

    steb Member

    Joined:
    Nov 15, 2016
    Messages:
    215
    Likes Received:
    66
    Trophy Points:
    28


    Detected ba to ng xigncode3 ? ginamit ko ung proxycall ni silverdeath gumana .
     

Share This Page

Top