Thread: Changing the key

  1. #1
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318

    Changing the key

    This code should change the key, so when the user enters a, then it shows f. But somehow it doesn't work.
    Code:
    #define EXPORT_HOOK_FUNCS
    #include"dll.h"
    #pragma data_seg(".hook_data")
    HHOOK HookKeyboard = 0;
    HWND  g_hAppWnd       = 0;
    #pragma data_seg()
    #pragma comment(linker, "/SECTION:.hook_data,RWS")
    
    BOOL WINAPI DllMain(HINSTANCE hinstDLL,DWORD,LPVOID){
    	return TRUE;
    }
    
    HOOK_DECLSPEC BOOL InstallHook(HWND hAppWnd) {
    	if (HookKeyboard != 0){
    		[B]return[/COLOR] FALSE;
    	}
    	g_hAppWnd = hAppWnd;
    	char buffer[256];
    	HookKeyboard = SetWindowsHookEx(WH_KEYBOARD_LL,(HOOKPROC)KeyboardProc, GetModuleHandle(NULL), 0);
    	if (HookKeyboard == 0) {
    		return FALSE;
    	}
    	return TRUE;
    }
    
    HOOK_DECLSPEC void UninstallHook(){
    	UnhookWindowsHookEx(HookKeyboard);
    	g_hAppWnd       = 0;
    	HookKeyboard = 0;
    }
    
    HOOK_DECLSPEC LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam,LPARAM lParam){
    	KBDLLHOOKSTRUCT *p = (KBDLLHOOKSTRUCT *) lParam;
    	if(p->vkCode==0x41){
    		p->vkCode=0x45;
    	}
    	return CallNextHookEx(HookKeyboard, nCode, wParam, lParam);
    }
    Last edited by maxorator; 09-22-2006 at 09:16 AM.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    And why in the world would you want that?

    Hooking the keyboard eh? For what?

  3. #3
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    I am trying to do a program that would help with other keyboard sets (eg. russian). I don't have a russian keyboard and I get some homeworks in russian, that are good to do with a computer. But... I can't find what does what. A is F on russian keyboard. That's exactly what's showed on the piece of code I posted.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  4. #4
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    This example may be of some help to you. I was just too lazy to write a DLL.

    Code:
    #include <windows.h>
    #include <stdio.h>
    
    const CHAR RussianAlphabet[] = 
       "zyxwvutsrqponmlkjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA";
    const CHAR EnglishAlphabet[] = 
      "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    
    LRESULT CALLBACK KeyboardProc(int code, WPARAM wParam,
        LPARAM lParam);
    
    HINSTANCE hInst;
    HHOOK hHook; 
    HWND hwndglobal; 
    
    LRESULT CALLBACK WindowFunc(HWND, UINT, WPARAM, LPARAM);
    char szWindowName[] = "MyWindow";
    
    int WINAPI WinMain(HINSTANCE hThisInst, HINSTANCE hPrevInst, 
                       LPSTR lpszArgs, int nWinMode)
    {
        HWND hwnd;
        MSG msg;
        WNDCLASSEX wcl;
        wcl.cbSize = sizeof(WNDCLASSEX); 
        wcl.hInstance = hThisInst;     
        wcl.lpszClassName = szWindowName; 
        wcl.lpfnWndProc = WindowFunc;  
        wcl.style = 0;
        wcl.hIcon = LoadIcon(NULL, IDI_APPLICATION); 
        wcl.hIconSm = NULL; 
        wcl.hCursor = LoadCursor(NULL, IDC_ARROW);
        wcl.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH); 
        wcl.cbClsExtra = 0;
        wcl.cbWndExtra = 0; 
        wcl.lpszMenuName = "KeyboardProc"; 
    
        if(!RegisterClassEx(&wcl)) return 0;
        hwnd = CreateWindow(szWindowName,
            "Demo a keyboard hook, translate from English to Russian",
            WS_OVERLAPPEDWINDOW, 
            CW_USEDEFAULT, CW_USEDEFAULT,
            CW_USEDEFAULT, CW_USEDEFAULT,
            NULL, NULL, hThisInst, NULL);
        hInst = hThisInst; 
        hwndglobal = hwnd;
        ShowWindow(hwnd, nWinMode);
        UpdateWindow(hwnd);
        while(GetMessage(&msg, NULL, 0, 0))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg); 
        }
        return msg.wParam;
    }
    
    LRESULT CALLBACK WindowFunc(HWND hwnd, UINT message,
        WPARAM wParam, LPARAM lParam)
    {
        HDC hdc;
        char str[255];
        TEXTMETRIC tm;
        SIZE size;
        static int X=0, Y=0;   
        static int maxX, maxY; 
        static HDC memdc;    
        static HBITMAP hbit; 
        HBRUSH hbrush;       
    
        switch(message) {
            case WM_CREATE:
                hHook = SetWindowsHookEx(WH_GETMESSAGE, (HOOKPROC) KeyboardProc,
                    NULL, GetCurrentThreadId());
                maxX = GetSystemMetrics(SM_CXSCREEN);
                maxY = GetSystemMetrics(SM_CYSCREEN);
                hdc = GetDC(hwnd);
                memdc = CreateCompatibleDC(hdc);
                hbit = CreateCompatibleBitmap(hdc, maxX, maxY);
                SelectObject(memdc, hbit);
                hbrush = (HBRUSH) GetStockObject(WHITE_BRUSH);
                SelectObject(memdc, hbrush);
                PatBlt(memdc, 0, 0, maxX, maxY, PATCOPY);
                ReleaseDC(hwnd, hdc);
                break;
            case WM_CHAR:
                hdc = GetDC(hwnd);
                GetTextMetrics(hdc, &tm);
                sprintf(str, "%c", (char) wParam); 
                if((char)wParam == '\r') {
                    Y = Y + tm.tmHeight + tm.tmExternalLeading;
                    X = 0; 
                }
                else {
                    TextOut(memdc, X, Y, str, 1); 
                    TextOut(hdc, X, Y, str, 1); 
                    GetTextExtentPoint32(memdc, str, strlen(str), &size);
                    X += size.cx; 
                }
                ReleaseDC(hwnd, hdc);
                break;
            case WM_DESTROY:
                UnhookWindowsHookEx(hHook);
                DeleteDC(memdc);
                DeleteObject(hbit);
                PostQuitMessage(0);
                break;
            default:
                return DefWindowProc(hwnd, message, wParam, lParam);
        }
        return 0;
    }
    
    LRESULT CALLBACK KeyboardProc(int code, WPARAM wParam, LPARAM lParam)
    {
        MSG *msg;
        msg = (MSG *) lParam;
        if(code >= 0) 
            switch(msg->message) {
                case WM_CHAR:
                    LPTSTR lptstrChar =  strchr(RussianAlphabet,  msg->wParam);
                    if( lptstrChar )
                    {
                        size_t offset = lptstrChar - RussianAlphabet;
                        msg->wParam = (WPARAM) EnglishAlphabet[offset];
                    }
            }
        return CallNextHookEx(hHook, code, wParam, lParam);
    }

  5. #5
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    I just changed my DLL into this by your example, but still doesn't work:
    Code:
    #define EXPORT_HOOK_FUNCS
    #include "dll.h"
    #pragma data_seg(".hook_data")
    HHOOK HookKeyboard = 0;
    HWND  g_hAppWnd       = 0;
    #pragma data_seg()
    #pragma comment(linker, "/SECTION:.hook_data,RWS")
    const CHAR RussianAlphabet[] = 
       "zyxwvutsrqponmlkjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA";
    const CHAR EnglishAlphabet[] = 
      "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    BOOL WINAPI DllMain(HINSTANCE hinstDLL,DWORD,LPVOID){
    	return TRUE;
    }
    
    HOOK_DECLSPEC BOOL InstallHook(HWND hAppWnd) {
    	if (HookKeyboard != 0){
    		return FALSE;
    	}
    	g_hAppWnd = hAppWnd;
    	char buffer[256];
    	HookKeyboard = SetWindowsHookEx(WH_GETMESSAGE,(HOOKPROC)KeyboardProc, GetModuleHandle(NULL), 0);
    	if (HookKeyboard == 0) {
    		return FALSE;
    	}
    	return TRUE;
    }
    
    HOOK_DECLSPEC void UninstallHook(){
    	UnhookWindowsHookEx(HookKeyboard);
    	g_hAppWnd       = 0;
    	HookKeyboard = 0;
    }
    
    HOOK_DECLSPEC LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam,LPARAM lParam){
        MSG *msg;
        msg = (MSG *) lParam;
        if(nCode >= 0) 
            switch(msg->message) {
                case WM_CHAR:
                    LPTSTR lptstrChar =  strchr(RussianAlphabet,  msg->wParam);
                    if( lptstrChar )
                    {
                        size_t offset = lptstrChar - RussianAlphabet;
                        msg->wParam = (WPARAM) EnglishAlphabet[offset];
                    }
            }
        MessageBox(g_hAppWnd,"aa","aa",MB_OK);
        return CallNextHookEx(HookKeyboard, nCode, wParam, lParam);
    }
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  6. #6
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    The only way it would work is this, but I don't want to use this method:
    Code:
    #define EXPORT_HOOK_FUNCS
    #include "dll.h"
    #pragma data_seg(".hook_data")
    HHOOK HookKeyboard = 0;
    HWND  hwnd       = 0;
    #pragma data_seg()
    #pragma comment(linker, "/SECTION:.hook_data,RWS")
    
    BOOL WINAPI DllMain(HINSTANCE hinstDLL,DWORD,LPVOID){
    	return TRUE;
    }
    
    HOOK_DECLSPEC BOOL InstallHook(HWND hAppWnd) {
    	if (HookKeyboard != 0){
    		return FALSE;
    	}
    	hwnd = hAppWnd;
    	char buffer[256];
    	HookKeyboard = SetWindowsHookEx(WH_KEYBOARD_LL,(HOOKPROC)KeyboardProc, GetModuleHandle(NULL), 0);
    	if (HookKeyboard == 0) {
    		return FALSE;
    	}
    	return TRUE;
    }
    
    HOOK_DECLSPEC void UninstallHook(){
    	UnhookWindowsHookEx(HookKeyboard);
    	hwnd       = 0;
    	HookKeyboard = 0;
    }
    
    HOOK_DECLSPEC LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam,LPARAM lParam){
    	char buffer[256];
    	if(nCode==HC_ACTION){
    		if(((KBDLLHOOKSTRUCT*)lParam)->vkCode==0x41){
    			INPUT ms;
    			ms.type=INPUT_KEYBOARD;
    			ms.ki.wVk=0x46;
    			if(wParam==WM_KEYDOWN){
    				ms.ki.dwFlags=0;
    			}
    			else{
    				ms.ki.dwFlags=KEYEVENTF_KEYUP;
    			}
    			SendInput(1,&ms,sizeof(INPUT));
    			return -1;
    		}
    	}
        return CallNextHookEx(HookKeyboard, nCode, wParam, lParam);
    }
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  7. #7
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Quote Originally Posted by maxorator
    I am trying to do a program that would help with other keyboard sets (eg. russian). I don't have a russian keyboard and I get some homeworks in russian, that are good to do with a computer. But... I can't find what does what. A is F on russian keyboard. That's exactly what's showed on the piece of code I posted.
    Why not just go into Regional and Language options and add Russian as an input language? Then you should, with a click of the mouse or hotkey, be able to use your keyboard as if it was a Russian keyboard. You might want to get out a fine-tipped marker and write the Cyrillic characters on your keys, though, until you get used to the new layout and never need to look.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  8. #8
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    I am not going to write in russian very much. I need to do some homeworks with it (maybe once in two weeks). And I also don't want to ruin my keyboard. I want to do so, that russian A would come, when I press my A. That means, I still have to use Regional and Language options.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  2. Virtual keys
    By Arkanos in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2005, 10:00 AM
  3. Directional Keys - Useing in Console
    By RoD in forum C++ Programming
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM
  4. FAQ: Directional Keys - Useing in Console
    By RoD in forum FAQ Board
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM