Author Topic: G15 Keyboard  (Read 6922 times)

0 Members and 1 Guest are viewing this topic.

Offline Paul

  • Pentium
  • *****
  • Posts: 1490
  • Karma: 47
    • View Profile
G15 Keyboard
« on: December 26, 2007 »
Hi
I got a Logitech G15 keyboard for Christmas and now I'm trying to program it. I'ts got a monochrome graphical LCD screen.
I've managed to get a simple program going in c++, but since i prefer Blitz3D I would like to create a dll with some simple functions.


This works
Code: [Select]
lgLcdOpenContext InnitLCD(LPCSTR name)
{
DWORD res;

    //// initialize the library
    res=lgLcdInit();//res
    HandleError(res, _T("lgLcdInit"));
   
    //// connect to LCDMon
    // set up connection context
    lgLcdConnectContext connectContext;
    ZeroMemory(&connectContext, sizeof(connectContext));
    connectContext.appFriendlyName = _T(name);//"simple sample");
    connectContext.isAutostartable = false;
    connectContext.isPersistent = false;
    // we don't have a configuration screen
    connectContext.onConfigure.configCallback = NULL;
    connectContext.onConfigure.configContext = NULL;
    // the "connection" member will be returned upon return
    connectContext.connection = LGLCD_INVALID_CONNECTION;
    // and connect
    res=lgLcdConnect(&connectContext);//RES
    HandleError(res, _T("lgLcdConnect"));

    // now we are connected (and have a connection handle returned),
    // let's enumerate an LCD (the first one, index = 0)
    lgLcdDeviceDesc deviceDescription;
    res=lgLcdEnumerate(connectContext.connection, 0, &deviceDescription);//res
    HandleError(res, _T("lgLcdEnumerate"));

    // at this point, we have an LCD
    //_tprintf(_T("Found an LCD with %dx%d pixels, %d bits per pixel and %d soft buttons\n"),
    //    deviceDescription.Width, deviceDescription.Height, deviceDescription.Bpp,
    //    deviceDescription.NumSoftButtons);

// open it
    lgLcdOpenContext openContext;
    ZeroMemory(&openContext, sizeof(openContext));
    openContext.connection = connectContext.connection;
    openContext.index = 0;
    // we have no softbutton notification callback
    openContext.onSoftbuttonsChanged.softbuttonsChangedCallback = NULL;
    openContext.onSoftbuttonsChanged.softbuttonsChangedContext = NULL;
    // the "device" member will be returned upon return
    openContext.device = LGLCD_INVALID_DEVICE;
    res=lgLcdOpen(&openContext);//res
    HandleError(res, _T("lgLcdEnumerate"));


return openContext;
}
But since Blitz cant get the returned class i need to send it to my function, and let c++ edit the content
This code here doesn't work though
Code: [Select]
void InnitLCD(LPCSTR name,lgLcdOpenContext openContext)
{
DWORD res;

    //// initialize the library
    res=lgLcdInit();//res
    HandleError(res, _T("lgLcdInit"));
   
    //// connect to LCDMon
    // set up connection context
    lgLcdConnectContext connectContext;
    ZeroMemory(&connectContext, sizeof(connectContext));
    connectContext.appFriendlyName = _T(name);//"simple sample");
    connectContext.isAutostartable = TRUE;
    connectContext.isPersistent = TRUE;
    // we don't have a configuration screen
    connectContext.onConfigure.configCallback = NULL;
    connectContext.onConfigure.configContext = NULL;
    // the "connection" member will be returned upon return
    connectContext.connection = LGLCD_INVALID_CONNECTION;
    // and connect
    res=lgLcdConnect(&connectContext);//RES
    HandleError(res, _T("lgLcdConnect"));

    // now we are connected (and have a connection handle returned),
    // let's enumerate an LCD (the first one, index = 0)
    lgLcdDeviceDesc deviceDescription;
    res=lgLcdEnumerate(connectContext.connection, 0, &deviceDescription);//res
    HandleError(res, _T("lgLcdEnumerate"));

    // at this point, we have an LCD
    //_tprintf(_T("Found an LCD with %dx%d pixels, %d bits per pixel and %d soft buttons\n"),
    //    deviceDescription.Width, deviceDescription.Height, deviceDescription.Bpp,
    //    deviceDescription.NumSoftButtons);

// open it
   // lgLcdOpenContext openContext;
    ZeroMemory(&openContext, sizeof(openContext));
    openContext.connection = connectContext.connection;
    openContext.index = 0;
    // we have no softbutton notification callback
    openContext.onSoftbuttonsChanged.softbuttonsChangedCallback = NULL;
    openContext.onSoftbuttonsChanged.softbuttonsChangedContext = NULL;
    // the "device" member will be returned upon return
    openContext.device = LGLCD_INVALID_DEVICE;
    res=lgLcdOpen(&openContext);//res
    HandleError(res, _T("lgLcdEnumerate"));


//return openContext;
}
So how would i edit the fisrts peice of code so i can call it like this:
Code: [Select]
lgLcdOpenContext openContext;
memset(&openContext,0,sizeof(openContext));
InnitLCD(_T("NAME"),openContext);
« Last Edit: December 27, 2007 by Paul »
I will bite you - http://s5.bitefight.se/c.php?uid=31059
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: G15 Keyboard
« Reply #1 on: December 26, 2007 »
You need to look up the definition of lgLcdOpenContext and work out how big it needs to be.  Then create a bank of that size and use the bank handle as the address to pass
openContext = CreateBank(...)
...set all entries in the bank to 0
InnitLCD("NAME",openContext);
...now you need to use Peek to grab the returned values back out of the bank.
This all relies on the undocumented fact that CreateBank returns a pointer to the memory!

Jim
Challenge Trophies Won:

Offline Paul

  • Pentium
  • *****
  • Posts: 1490
  • Karma: 47
    • View Profile
Re: G15 Keyboard
« Reply #2 on: December 27, 2007 »
Ty jim, but i realised that i posted the wrong code, so please have a look again :S
Ill try to clarify a little bit more aswell:
I've got the function lgLcdOpenContext InnitLCD(LPCSTR name) working, but since blitz cant take returned banks, arrays or types i need a function like this:
void InnitLCD(LPCSTR name,lgLcdOpenContext openContext) where i can send a bank to my dll.
The problem is that i don't seem to get the conversion right.
I will bite you - http://s5.bitefight.se/c.php?uid=31059
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: G15 Keyboard
« Reply #3 on: December 28, 2007 »
I see.  I think you need to make it
Code: [Select]
void InnitLCD(LPCSTR name, lgLcdOpenContext *openContext)
So it's taking a pointer to the context.

Jim
Challenge Trophies Won:

Offline zawran

  • Sponsor
  • Pentium
  • *******
  • Posts: 909
  • Karma: 67
    • View Profile
Re: G15 Keyboard
« Reply #4 on: December 28, 2007 »
A german guy made a dll that can be used with Blitz3D, so you might want to check that one out.

http://www.blitzforum.de/forum/viewtopic.php?t=26391

Offline Paul

  • Pentium
  • *****
  • Posts: 1490
  • Karma: 47
    • View Profile
Re: G15 Keyboard
« Reply #5 on: December 28, 2007 »
Thanks got it to work  :carrot:
This is my first real attempt to do something in cpp.
I will bite you - http://s5.bitefight.se/c.php?uid=31059
Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17409
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: G15 Keyboard
« Reply #6 on: December 29, 2007 »
And you're using it to program the lcd display on your keyboard? :)

Hehe, I love goofy stuff like this have some good Karma.
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Paul

  • Pentium
  • *****
  • Posts: 1490
  • Karma: 47
    • View Profile
Re: G15 Keyboard
« Reply #7 on: December 30, 2007 »
Ty, one more question:
To get the volume wheel input without changing the volume (get the input to my program and stopping windows from changing the volume) I'm setting a low level keyboard hook. the problem is that I've only got it to work in a Windows application and not in my dll. Is there something specific that i need to include to the dll?

here is my function:
Code: [Select]
static HHOOK hHook = NULL;

LRESULT CALLBACK HookProc (int nCode, WPARAM wParam, LPARAM lParam)
{
   if (nCode == HC_ACTION)
   {
      LPKBDLLHOOKSTRUCT kbhs = (LPKBDLLHOOKSTRUCT)lParam;
      if ((kbhs->vkCode == VK_VOLUME_DOWN || kbhs->vkCode == VK_VOLUME_UP) && wParam == WM_KEYDOWN)
  {
MessageBoxA(0,"Someone rolled the volume wheel","HI",0);
         return 1;
      }
   }
    return CallNextHookEx(hHook, nCode, wParam, lParam);
}
I will bite you - http://s5.bitefight.se/c.php?uid=31059
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: G15 Keyboard
« Reply #8 on: December 30, 2007 »
How is the code that sets up the hook called?  Can you check you are getting any hooked messages at all?  Maybe the keyboard driver is in the hook chain before yours?  Just some ideas.

Jim
Challenge Trophies Won:

Offline Paul

  • Pentium
  • *****
  • Posts: 1490
  • Karma: 47
    • View Profile
Re: G15 Keyboard
« Reply #9 on: December 31, 2007 »
I'm running it like this:
Code: [Select]
hHook = SetWindowsHookEx(WH_KEYBOARD_LL, (HOOKPROC)HookProc, 0, 0);
And freeing it like this:
Code: [Select]
UnhookWindowsHookEx(hHook);
wehre hHook is a static
and as i said, it works in the "Windows Project", but not in the "Dll". I'm using M$ vc++ 2005 express edition, maybe has something to do with that.

My Hook procedure doesn't seem to get called at all, as if there where some kind of block.


edit:
forgot to mention that i get a few errors if i just copy the functions to the dll:
1>.\dllmain.cpp(87) : error C2065: 'WH_KEYBOARD_LL' : undeclared identifier
1>.\dllmain.cpp(208) : error C2065: 'LPKBDLLHOOKSTRUCT' : undeclared identifier

1>.\dllmain.cpp(209) : error C2065: 'VK_VOLUME_DOWN' : undeclared identifier
1>.\dllmain.cpp(209) : error C2065: 'VK_VOLUME_UP' : undeclared identifier

and various others because of these, if i define all these it compies but windows doesn't seem to call my hook procedure.
« Last Edit: December 31, 2007 by Paul »
I will bite you - http://s5.bitefight.se/c.php?uid=31059
Challenge Trophies Won:

Offline Paul

  • Pentium
  • *****
  • Posts: 1490
  • Karma: 47
    • View Profile
Re: G15 Keyboard
« Reply #10 on: December 31, 2007 »
Added this before "include <windows.h>", and it worked
Code: [Select]
#pragma once

// Modify the following defines if you have to target a platform prior to the ones specified below.
// Refer to MSDN for the latest info on corresponding values for different platforms.
#ifndef WINVER // Allow use of features specific to Windows XP or later.
#define WINVER 0x0501 // Change this to the appropriate value to target other versions of Windows.
#endif

#ifndef _WIN32_WINNT // Allow use of features specific to Windows XP or later.                   
#define _WIN32_WINNT 0x0501 // Change this to the appropriate value to target other versions of Windows.
#endif

#ifndef _WIN32_WINDOWS // Allow use of features specific to Windows 98 or later.
#define _WIN32_WINDOWS 0x0410 // Change this to the appropriate value to target Windows Me or later.
#endif

#ifndef _WIN32_IE // Allow use of features specific to IE 6.0 or later.
#define _WIN32_IE 0x0600 // Change this to the appropriate value to target other versions of IE.
#endif
#define WIN32_LEAN_AND_MEAN

but it isn't fast, probably not meant to have a volume wheel, only keys
If i scroll from 0-100 fast it continues to over 230 :D but i can live with that if there isn't an easy solution.
I will bite you - http://s5.bitefight.se/c.php?uid=31059
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: G15 Keyboard
« Reply #11 on: December 31, 2007 »
I suspect you only need _WINVER set.  And I also suspect you get the raw events, hence the 'random' values.

Jim
Challenge Trophies Won:

Offline Paul

  • Pentium
  • *****
  • Posts: 1490
  • Karma: 47
    • View Profile
Re: G15 Keyboard
« Reply #12 on: February 12, 2008 »
Hi again...

Dont know if i should have started a new topic but I'll try it like here.

I'm trying to get the hook thing to work in freebasic, but something is wrong and i can't find it.
Windows doesn't seem to run my hookproc at all. one of my attempts to solve the problem was to dump in all then defines that i could find, but it didn't help.

Code: [Select]
'#pragma once

' Modify the following defines if you have to target a platform prior to the ones specified below.
' Refer to MSDN for the latest info on corresponding values for different platforms.
#ifndef WINVER ' Allow use of features specific to Windows XP or later.
#define WINVER 0x0501 ' Change this to the appropriate value to target other versions of Windows.
#endif

#ifndef _WIN32_WINNT ' Allow use of features specific to Windows XP or later.                   
#define _WIN32_WINNT 0x0501 ' Change this to the appropriate value to target other versions of Windows.
#endif

#ifndef _WIN32_WINDOWS ' Allow use of features specific to Windows 98 or later.
#define _WIN32_WINDOWS 0x0410 ' Change this to the appropriate value to target Windows Me or later.
#endif

#ifndef _WIN32_IE ' Allow use of features specific to IE 6.0 or later.
#define _WIN32_IE 0x0600 ' Change this to the appropriate value to target other versions of IE.
#endif
#define WIN32_LEAN_AND_MEAN
#define WIN_INCLUDEALL


#include "windows.bi"


option explicit
option static


DECLARE function HookProc (nCode as integer, wParam as WPARAM, lParam as LPARAM) as LRESULT



dim shared hHook as long    'Handle to the keyboard proc

hHook=SetWindowsHookEx(WH_KEYBOARD_LL,@hookproc,0,0)
if hHook=0 then print "failed to create hook"



dim oks as integer          'the state escape was in when the program was started
print "Press escape to end"
oks=getkeystate(vk_escape)
while getkeystate(vk_escape)=oks
   
wend

UnhookWindowsHookEx(@hookproc)  'free the hook








function HookProc (nCode as integer, wParam as WPARAM, lParam as LPARAM) as LRESULT
    if (nCode = HC_ACTION) then
        'original code
        'LPKBDLLHOOKSTRUCT kbhs = (LPKBDLLHOOKSTRUCT)lParam
       
        'FB converions(I hope)
         dim kbhs as LPKBDLLHOOKSTRUCT' = lParam
        'kbhs=cast(LPKBDLLHOOKSTRUCT,lParam)
        kbhs=lParam
       
       
        if ((kbhs->vkCode = VK_VOLUME_DOWN or kbhs->vkCode = VK_VOLUME_UP) and wParam = WM_KEYDOWN) then
            print "hi"
            return 1
        endif
    endif
    print "A"
    return CallNextHookEx(hHook, nCode, wParam, lParam)
end function
[code]
[/code]
I will bite you - http://s5.bitefight.se/c.php?uid=31059
Challenge Trophies Won: