Skip to content

Instantly share code, notes, and snippets.

@hasteagag
Created March 9, 2023 02:44
Show Gist options
  • Save hasteagag/1c48ef79e1f2d16a1999eb7aafafce6b to your computer and use it in GitHub Desktop.
Save hasteagag/1c48ef79e1f2d16a1999eb7aafafce6b to your computer and use it in GitHub Desktop.
quick tweak on events Example from Corsair SDK to make G1 G3 and so on (column shift)
/******************************************************************************
**
** File main.cpp
** Author Denys Romanenko
** Copyright (c) 2021, Corsair Memory, Inc. All Rights Reserved.
**
** This file is part of Corsair SDK Lighting Effects.
**
******************************************************************************/
#ifdef __APPLE__
#include <CUESDK/CUESDK.h>
#else
#include <CUESDK.h>
#endif
#include <string>
#include <unordered_map>
#include <iostream>
//#include <cstdio>
//#include <fstream>
#include <windows.h>
#include <WinBase.h>
#include <algorithm>
#include <chrono>
#include <atomic>
#include <thread>
const std::unordered_map<CorsairKeyId, std::string> keyIdStrings = {
{CorsairKey_Invalid, "CorsairKey_Invalid"},
{CorsairKeyKb_G1, "CorsairKeyKb_G1"},
{CorsairKeyKb_G2, "CorsairKeyKb_G2"},
{CorsairKeyKb_G3, "CorsairKeyKb_G3"},
{CorsairKeyKb_G4, "CorsairKeyKb_G4"},
{CorsairKeyKb_G5, "CorsairKeyKb_G5"},
{CorsairKeyKb_G6, "CorsairKeyKb_G6"},
{CorsairKeyKb_G7, "CorsairKeyKb_G7"},
{CorsairKeyKb_G8, "CorsairKeyKb_G8"},
{CorsairKeyKb_G9, "CorsairKeyKb_G9"},
{CorsairKeyKb_G10, "CorsairKeyKb_G10"},
{CorsairKeyKb_G11, "CorsairKeyKb_G11"},
{CorsairKeyKb_G12, "CorsairKeyKb_G12"},
{CorsairKeyKb_G13, "CorsairKeyKb_G13"},
{CorsairKeyKb_G14, "CorsairKeyKb_G14"},
{CorsairKeyKb_G15, "CorsairKeyKb_G15"},
{CorsairKeyKb_G16, "CorsairKeyKb_G16"},
{CorsairKeyKb_G17, "CorsairKeyKb_G17"},
{CorsairKeyKb_G18, "CorsairKeyKb_G18"},
{CorsairKeyMouse_M1, "CorsairKeyMouse_M1"},
{CorsairKeyMouse_M2, "CorsairKeyMouse_M2"},
{CorsairKeyMouse_M3, "CorsairKeyMouse_M3"},
{CorsairKeyMouse_M4, "CorsairKeyMouse_M4"},
{CorsairKeyMouse_M5, "CorsairKeyMouse_M5"},
{CorsairKeyMouse_M6, "CorsairKeyMouse_M6"},
{CorsairKeyMouse_M7, "CorsairKeyMouse_M7"},
{CorsairKeyMouse_M8, "CorsairKeyMouse_M8"},
{CorsairKeyMouse_M9, "CorsairKeyMouse_M9"},
{CorsairKeyMouse_M10, "CorsairKeyMouse_M10"},
{CorsairKeyMouse_M11, "CorsairKeyMouse_M11"},
{CorsairKeyMouse_M12, "CorsairKeyMouse_M12"}
};
const char *toString(CorsairError error)
{
switch (error) {
case CE_Success:
return "CE_Success";
case CE_ServerNotFound:
return "CE_ServerNotFound";
case CE_NoControl:
return "CE_NoControl";
case CE_ProtocolHandshakeMissing:
return "CE_ProtocolHandshakeMissing";
case CE_IncompatibleProtocol:
return "CE_IncompatibleProtocol";
case CE_InvalidArguments:
return "CE_InvalidArguments";
default:
return "unknown error";
}
}
bool errorCheck(const std::string &msg) {
auto error = CorsairGetLastError();
if (error != CorsairError::CE_Success) {
std::cerr << msg << " (Error: " << toString(error) << ')' << std::endl;
return true;
}
return false;
}
void presserFunc(CorsairKeyId keyId, bool isUp)
{
int ud = 0;
isUp ? (ud = 0) : (ud = 2); // int should be fine here as hex and dec are same //KEYEVENTF_KEYUP = 0x0002
//here is where I need the bucky bit to determine what we are doing.
std::cout << "0 keyId is " << keyId << std::endl;
int arbitrarySCoffset = 0;
if (keyId <= 26)
{
arbitrarySCoffset = 192 + keyId;
}
else if (keyId >= 27 && keyId <= 30)
{ //this is only so wierd bc some of it was used for other things according to MS Docs, this is only for higher mouse keys
arbitrarySCoffset = 206 + keyId;
}
else {
std::cout << "error line 143 - keyID was out of bounds";
return;
}
std::cout << "1 arbitrarySCoffset is " << arbitrarySCoffset << std::endl;
switch (arbitrarySCoffset)
{
case 193:
arbitrarySCoffset = 195;
break;
case 194:
arbitrarySCoffset = 198;
break;
case 195:
arbitrarySCoffset = 201;
break;
case 196:
arbitrarySCoffset = 204;
break;
case 197:
arbitrarySCoffset = 207;
break;
case 198:
arbitrarySCoffset = 210;
break;
default:
std::cout << "3 hit default" << std::endl;
}
std::cout << "2 arbitrarySCoffset is " << arbitrarySCoffset << std::endl;
keybd_event(VK_KANA, arbitrarySCoffset, ud, 0);
return;
}
class EventPrinter
{
public:
void print(const CorsairEvent *e)
{
std::cout << "Event #" << std::to_string(++m_eventCounter) << std::endl;
if (e->id == CEI_DeviceConnectionStatusChangedEvent) {
printConnectionEvent(e->deviceConnectionStatusChangedEvent);
} else if (e->id == CEI_KeyEvent) {
printKeyEvent(e->keyEvent);
handleKeyEvent(e->keyEvent);
} else {
std::cout << "Invalid event!" << std::endl;
}
}
private:
void printConnectionEvent(const CorsairDeviceConnectionStatusChangedEvent *e) const
{
std::cout << "Device id: " << e->deviceId
<< " Status: " << (e->isConnected ? "connected" : "disconnected") << std::endl;
}
void printKeyEvent(const CorsairKeyEvent *e) const
{
std::cout << " Device id: " << e->deviceId
<< " Key id: " << keyIdStrings.at(e->keyId)
<< " Key state: " << (e->isPressed ? "pressed" : "released") << std::endl;
}
void handleKeyEvent(const CorsairKeyEvent* e) const
{
presserFunc(e->keyId, e->isPressed);
}
private:
int m_eventCounter = 0;
};
int main(int argc, char *argv[])
{
CorsairPerformProtocolHandshake();
while (const auto error = CorsairGetLastError()) {
CorsairPerformProtocolHandshake();
std::cout << "Handshake failed: " << toString(error) << "\nWe will be retrying every 10 seconds, until the process is killed or is otherwise successful." << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(10));
}
/* if (errorCheck("Handshake error")) {
getchar();
return -1;
}*/
const auto callback = [](void *context, const CorsairEvent *e) {
EventPrinter *eventPrinter = static_cast<EventPrinter*>(context);
eventPrinter->print(e);
};
//Context could be any class instance or any pointer. Client must ensure context is valid during callback execution
EventPrinter context;
CorsairSubscribeForEvents(callback, &context);
if (errorCheck("Subscribe for events error")) {
getchar();
return -1;
}
std::cout << "Working... Press any G/M key or connect/disconnect Corsair device to see events in action" << std::endl;
std::cout << "Press \"q\" to close program..." << std::endl;
while (true) {
char c = getchar();
if (c == 'q' || c == 'Q') {
break;
}
}
CorsairUnsubscribeFromEvents();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment