Sign in to follow this  

[.net] Which keyboard sent the input?

This topic is 4719 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey, I've got one question: I have a keyboard and a remote control connected to the computer, which both are handled as normal keyboards by Windows XP. Is there a way of determining, which of those two devices sent the input in .NET? So when i press the "1" on my remotecontrol i want to be able to know, that it was my remote control and not my keyboard. THX in advance Metzler

Share this post


Link to post
Share on other sites
Quote:
Original post by Metzler
Hey,

I've got one question: I have a keyboard and a remote control connected to the computer, which both are handled as normal keyboards by Windows XP.
Is there a way of determining, which of those two devices sent the input in .NET?
So when i press the "1" on my remotecontrol i want to be able to know, that it was my remote control and not my keyboard.

THX in advance

Metzler


As far as I know, that functionality is not part of the .NET Framework. DirectInput/Managed DirectX is probably the way to go here, as the above poster suggested.

Share this post


Link to post
Share on other sites
So i just tried listing all Input Devices with the Manager class, but this particular one cant be found...
Any idea, guys?
In the device manager, both are listed...

THX in advance

Share this post


Link to post
Share on other sites
Some input devices don't work with directx or in the case of my tablet only expose a tiny subset of their functionality. It may totaly be overkill but check and see if the maker of your input device supplies some library or works with some standard library (for my tablet there was wintab). If you can find a lib than you may be able to use pinvoke to access the input device through that lib.

Also: all of you compatable devices should show up in the DirectX caps viewer -- but that should be the same as your EnumerateDevices call.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Just guessing here... But this is what I would try myself.

When your application start, enumerate all (your 2) keyboards and save away IDirectInputDeviceX pointers to them.

In your WM_KEYDOWN handler, call IDirectInputDevice8::GetDeviceState, which returns a byte array with each key's state. You'd have to look in the array for your remote control to see if that array indicates that the whatever key was pressed, or if it was pressed by another keyboard.

http://msdn.microsoft.com/library/en-us/directx9_c/directx/input/ref/ifaces/idirectinputdevice9/GetDeviceState.asp


Please post result if you try this.

Share this post


Link to post
Share on other sites
If DirectInput is really treating the remote as a keyboard, then you will not be able to get it as a separate device. Unfortunately, DirectInput has stopped supporting separate enumeration of keyboard and mouse devices: all keyboard devices are combined into the system keyboard device, and all mouse devices are combined into the system mouse device. DirectInput most likely is not the answer. If someone knows a way around this, please say so because I have done extensive research on the subject for my own input system, and everything I have seen indicates that it is impossible under XP with DirectInput.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by Metzler
How can i store a to a device in directx, which cannot be found by directx?
I am only able to find it with WMI.
Just to confirm... So if you call DirectInput8Create(), and then IDirectInput8::EnumDevices(DI8DEVCLASS_KEYBOARD, ...), you will only get 1 callback for the system keyboard, even though you have 2 keyboards attached (i.e. the system keyboard and your remote)?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by Metzler
Ok, just saw, that managed DirectX doesnt have the DirectInput8Create function anymore, as it seems. I couldnt find anything like it in the sdk docu...
Here's what I tried with this SDK: http://www.microsoft.com/downloads/details.aspx?FamilyID=4e825a37-0c94-4421-9ec8-156e52525d11&displaylang=en

Link with "d3d8.lib" and "dinput8.lib".

Sorry if formatting below gets screwed :(


---------------------
[code]
#define _WIN32_WINNT 0x0501

#define DIRECTINPUT_VERSION 0x0800

#include
#include

#include

#define INITGUID
#include

#include
#include

#include


CComModule _Module;

CComPtr directInput;
CComPtr inputDevice;


class CMainWindow : public CWindowImpl
{
public:
CMainWindow()
{
ZeroMemory(&m_buttons, sizeof(m_buttons));
}

private:
CMainWindow(const CMainWindow&);
CMainWindow& operator=(const CMainWindow&);

BEGIN_MSG_MAP(CMainWindow)
MESSAGE_HANDLER(WM_PAINT, OnPaint)
MESSAGE_HANDLER(WM_CREATE, OnCreate)
MESSAGE_HANDLER(WM_ACTIVATE, OnActivate)
MESSAGE_HANDLER(WM_DESTROY, OnDestroy)
MESSAGE_HANDLER(WM_KEYDOWN, OnKeyDown)
MESSAGE_HANDLER(WM_KEYUP, OnKeyUp)
END_MSG_MAP()

LRESULT OnActivate(UINT ,
WPARAM wParam,
LPARAM lParam,
BOOL& bHandled)
{
UNREFERENCED_PARAMETER(bHandled);
UNREFERENCED_PARAMETER(lParam);

HRESULT hr;
if (LOWORD(wParam) == WA_INACTIVE)
{
hr = inputDevice->Unacquire();
}
else
{
hr = inputDevice->Acquire();
}
return 0;
}

byte m_buttons[256];

LRESULT OnKeyDown(UINT ,
WPARAM ,
LPARAM ,
BOOL&)
{
KeyChange();
return 0;
}

void KeyChange()
{
byte buttons[256] = {0};
HRESULT hr = inputDevice->GetDeviceState(256, &buttons[0]);
hr;
for (int index = 0; index SetCooperativeLevel(m_hWnd, DISCL_BACKGROUND|DISCL_NONEXCLUSIVE);
hr;
return 0;
}

LRESULT OnPaint(UINT, WPARAM, LPARAM, BOOL&)
{
PAINTSTRUCT ps;
HDC hDC = GetDC();
BeginPaint(&ps);

TCHAR buf[] = _T("test");
ExtTextOut(hDC, 20, 40, 0, NULL, buf, (UINT)_tcslen(buf), NULL);

EndPaint(&ps);
return 0;
}

LRESULT OnDestroy(UINT, WPARAM, LPARAM, BOOL&)
{
PostQuitMessage(0);
return 0;
}
};



class ComScope
{
public:
ComScope()
{
::CoInitializeEx(NULL, COINIT_MULTITHREADED);
}
~ComScope()
{
::CoUninitialize();
}
};

typedef std::list DeviceList;

BOOL CALLBACK EnumDevicesCallback(LPCDIDEVICEINSTANCE lpddi, void* ref)
{
DeviceList* pKeyboards = (DeviceList*) ref;
pKeyboards->push_back(*lpddi);
return TRUE;
}


int WINAPI _tWinMain(HINSTANCE instance, HINSTANCE , LPSTR , int)
{
ComScope comScope;
DeviceList keyboards;
CMainWindow mainWindow;
HRESULT hr;

_Module.Init(NULL, instance);

hr = DirectInput8Create(GetModuleHandle(NULL), DIRECTINPUT_VERSION,
IID_IDirectInput8, (VOID**)&directInput, NULL);

if (FAILED(hr))
{
goto lblExit;
}


// DI8DEVCLASS_ALL
// DI8DEVCLASS_KEYBOARD
hr = directInput->EnumDevices(DI8DEVCLASS_KEYBOARD, EnumDevicesCallback, &keyboards, DIEDFL_ATTACHEDONLY);
if (FAILED(hr))
{
goto lblExit;
}

// enumerate and print
for (DeviceList::const_iterator I = keyboards.begin();
I != keyboards.end();
++I)
{
const DIDEVICEINSTANCE& keyboard = *I;
OutputDebugString(keyboard.tszProductName);
OutputDebugString(_T(" - "));
OutputDebugString(keyboard.tszInstanceName);
OutputDebugString(_T("\n"));
}

// replace keyboard[0] with whatever keyboard you want
hr = directInput->CreateDevice(keyboards.begin()->guidInstance, &inputDevice, NULL);
if (FAILED(hr))
{
goto lblExit;
}

hr = inputDevice->SetDataFormat(&c_dfDIKeyboard);
if (FAILED(hr))
{
goto lblExit;
}

{
RECT rect = {0};
rect.right = 500;
rect.bottom = 500;
mainWindow.Create(0, rect, _T("test"), WS_OVERLAPPEDWINDOW|WS_VISIBLE);
}

MSG msg;
while(::GetMessage(&msg, NULL, 0, 0))
{
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}

lblExit:
_Module.Term();

return 0;
}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by Metzler
Nice.
And a C# way?
Sorry, although I'm daily working in C# I haven't done anything with managed directx :(

Have you seen this article: http://www.kuhnstall.de/tutorials/dx9sharp2.html

What do you see if you enumerate your keybaords?


foreach (DeviceInstance d in Manager.Devices)
{
Debug.WriteLine(d.DeviceType.ToString());
if (d.DeviceType == DeviceType.Keyboard)
{
// found keyboard
}
}

Share this post


Link to post
Share on other sites
That is what I tried to say earlier. You can have DirectInput enumerate all keyboard/mouse/joystick devices for you, but only joystick devices will ever return more than one device, even if more than one physical device is attached. I have tried it. I believe that there may have been a DirectInput version and operating system under which that was not true at one time, but DirectInput8 and Windows XP is not it. There are other, non-DirectInput methods that must be used in order to get separate handles to each physical keyboard and mouse under Windows XP.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I suggest you ask in Microsoft's newsgroups, where MS developers might be able to help you.


http://support.microsoft.com/newsgroups/default.aspx?newsgroup=microsoft.public.win32.programmer.directx.managed&SCLID=US&ICP=GSS3&sd=GN&id=fh;en-us;newsgroups

news://news.microsoft.com/microsoft.public.win32.programmer.directx.managed

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
perhaps
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/userinput/rawinput.asp

will help more. granted you will probably need to use P/Invoke to access the win32 api to use the functions, it will give individual access to any device on the system. This means you can have 5 keyboards, 10 mice and be quite content dealing with all the input seperatly.

i think your best bet would be to create a dll in c++ that contains the simplfied higher level functions for you to call to make thing much needer (or perhaps a wrapper object in c#).

Good luck.

Share this post


Link to post
Share on other sites

This topic is 4719 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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