ON KEY F12 does not work in HMG 3.3.1

Moderator: Rathinagiri

tiampei
Posts: 12
Joined: Tue Jun 24, 2014 2:47 am

Re: ON KEY F12 does not work in HMG 3.3.1

Post by tiampei »

Dev-C

Code: Select all

#include <windows.h>

/* This is where all the input to the window goes to */
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {
	switch(Message) {
		
		/* trap the WM_CLOSE (clicking X) message, and actually tell the window to close */
		case WM_CLOSE: {
			DestroyWindow(hwnd);
			break;
		}
		
		/* Upon destruction, tell the main thread to stop */
		case WM_DESTROY: {
			PostQuitMessage(0);
			break;
		}
		
		case WM_HOTKEY: {
			MessageBox(NULL, "Hotkey is press", "Hotkey", MB_ICONINFORMATION);
			break;
		}
		
		
		/* All other messages (a lot of them) are processed using default procedures */
		default:
			return DefWindowProc(hwnd, Message, wParam, lParam);
	}
	return 0;
}

/* The 'main' function of Win32 GUI programs: this is where execution starts */
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
	WNDCLASSEX wc; /* A properties struct of our window */
	HWND hwnd; /* A 'HANDLE', hence the H, or a pointer to our window */
	MSG Msg; /* A temporary location for all messages */

	int ID1 = 33000, ID2 = 33001, ID3 = 33002;

	/* zero out the struct and set the stuff we want to modify */
	memset(&wc,0,sizeof(wc));
	wc.cbSize		 = sizeof(WNDCLASSEX);
	wc.lpfnWndProc	 = WndProc; /* This is where we will send messages to */
	wc.hInstance	 = hInstance;
	wc.hCursor		 = LoadCursor(NULL, IDC_ARROW);
	
	/* White, COLOR_WINDOW is just a #define for a system color, try Ctrl+Clicking it */
	wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
	wc.lpszClassName = "WindowClass";
	wc.hIcon		 = LoadIcon(NULL, IDI_APPLICATION); /* Load a standard icon */
	wc.hIconSm		 = LoadIcon(NULL, IDI_APPLICATION);

	if(!RegisterClassEx(&wc)) {
		MessageBox(NULL, "Window Registration Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
		return 0;
	}

	hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,"WindowClass","Press F10 to F12 to test the hotkey",WS_VISIBLE|WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT, /* x */
		CW_USEDEFAULT, /* y */
		640, /* width */
		480, /* height */
		NULL,NULL,hInstance,NULL);

	if(hwnd == NULL) {
		MessageBox(NULL, "Window Creation Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
		return 0;
	}

	if (!RegisterHotKey(hwnd, ID1, 0, VK_F10))
	{
		MessageBox(NULL, "Register hotkey F10 fail", "Fail!!", MB_ICONERROR);
	}
	if (!RegisterHotKey(hwnd, ID2, 0, VK_F11))
	{
		MessageBox(NULL, "Register hotkey F11 fail", "Fail!!", MB_ICONERROR);
	}
	if (!RegisterHotKey(hwnd, ID3, 0, VK_F12))
	{
		MessageBox(NULL, "Register hotkey F12 fail", "Fail!!", MB_ICONERROR);
	}

	/*
		This is the heart of our program where all input is processed and 
		sent to WndProc. Note that GetMessage blocks code flow until it receives something, so
		this loop will not produre unreasonably CPU usage
	*/
	while(GetMessage(&Msg, NULL, 0, 0) > 0) { /* If no error is received... */
		TranslateMessage(&Msg); /* Translate keycodes to chars if present */
		DispatchMessage(&Msg); /* Send it to WndProc */
	}
	
	UnregisterHotKey(hwnd, ID1);
	UnregisterHotKey(hwnd, ID2);
	UnregisterHotKey(hwnd, ID3);
	
	return Msg.wParam;
}

trmpluym
Posts: 303
Joined: Tue Jul 15, 2014 6:52 pm
Location: The Netherlands

Re: ON KEY F12 does not work in HMG 3.3.1

Post by trmpluym »

Even better, i can use the F12 key in HMG using the HMG_GetLastVirtualKeyDown() in the ON KEY function using a GRID.
tiampei
Posts: 12
Joined: Tue Jun 24, 2014 2:47 am

Re: ON KEY F12 does not work in HMG 3.3.1

Post by tiampei »

trmpluym wrote:A lot of other programs make use of the F12 key. For example Microsoft Word 2010 uses F12 (save as) . When it was a Windows limitation the F12 key also would not work in other programs.

I also use xHarbour. In xHarbour i can also use the F12 in my xBase 32 bits executables.

So why not HMG ?
Not sure why others work. Hmg using RegisterHotKey function from windows. So, If that function fails, then the hotkey is NOT working.

There still got solution to solve the problem.

One of the solution is changing the UserDebuggerHotKey to others KEY.
http://muzso.hu/2011/12/13/setting-f12- ... in-windows
I have test it using windows 8 machine.

Details description of UserDebuggerHotKey from MSDN:
http://technet.microsoft.com/en-us/libr ... 86263.aspx
KDJ
Posts: 243
Joined: Mon Sep 05, 2016 3:04 am
Location: Poland

Re: ON KEY F12 does not work in HMG 3.3.1

Post by KDJ »

If F12 can not be register as hotkey, it should be removed from documentation and from i_keybd.ch.
User avatar
Pablo César
Posts: 4059
Joined: Wed Sep 08, 2010 1:18 pm
Location: Curitiba - Brasil

ON KEY F12 does not work in HMG

Post by Pablo César »

KDJ wrote: Sun Mar 12, 2017 4:05 pm If F12 can not be register as hotkey, it should be removed from documentation and from i_keybd.ch.
Hi Krzysztof,

It's always good to see your messages. :D

Regrettably I have to agree with you when the user can not register the F12 as HotKey. :|

Colleague Kek (tiampei) mentioned how to enable F12 on Windows in: http://muzso.hu/2011/12/13/setting-f12- ... in-windows

I have adapted the Theo example code with LOG alteration and it works! :D

Code: Select all

/*
   http://muzso.hu/2011/12/13/setting-f12-as-a-global-hotkey-in-windows
   http://www.hmgforum.com/viewtopic.php?p=49940#p49940
*/
#include <hmg.ch>

Function Main()
PRIVATE hexRegKey:=Win_RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AeDebug\UserDebuggerHotKey")
PRIVATE hexRegKeyOld:=hexRegKey

DEFINE WINDOW Win_1 AT 157 , 162 WIDTH 550 HEIGHT 350 ;
    TITLE "Press F11 and F12 function keys to test" MAIN ;
	ON RELEASE ProcedureOnRelease()

    ON KEY F11 OF Win_1 ACTION MsgInfo('TEST F11',"Always Ok")
    ON KEY F12 OF Win_1 ACTION MsgInfo('TEST F12',"Ok now")
	
	DEFINE LABEL Label_1
	    ROW    200
	    COL    40
	    WIDTH  520
	    HEIGHT 24
	    VALUE "Before you click the button, test F12 key: supposed to be working now !!"
	    FONTNAME "Arial"
	    FONTSIZE 10
	    FONTBOLD .T.
	    VISIBLE !(hexRegKey=0)
	    FONTCOLOR RED
	END LABEL
	
	DEFINE BUTTON Button_1
	    ROW    140
	    COL    60
	    WIDTH  400
	    HEIGHT 28
	    ACTION ChngReg()
	    CAPTION If(hexRegKey=0,"Change your UserDebuggerHotKey from your Windows REGISTER","Return back UserDebuggerHotKey value")
	END BUTTON
    
END WINDOW
CENTER WINDOW Win_1
ACTIVATE WINDOW Win_1
Return Nil

Function ChngReg()
LOCAL cNewCaption

If hexRegKey=0
   cNewCaption:="Return back UserDebuggerHotKey value"
   hexRegKey:=0x2F
Else
   cNewCaption:="Change your UserDebuggerHotKey from your Windows REGISTER"
   hexRegKey:=0
Endif
SetProperty("Win_1","Button_1","CAPTION",cNewCaption)
Return Nil

Function SetRegValueAdmin(cChngKey)
LOCAL cCurPath := cFilePath(hb_ProgName())
LOCAL cRunParam := cCurPath + "\MyRegWrite.vbs"
LOCAL cFileRun := GetSystemFolder() + "\CScript.exe"
LOCAL cRegText := 'Set wshShell = CreateObject( "WScript.Shell" )'+CRLF

cRegText := cRegText+'wshShell.RegWrite "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AeDebug\UserDebuggerHotKey", '+cChngKey+', "REG_DWORD"'+CRLF+'Set wshShell = Nothing'
hb_MemoWrit(cRunParam,cRegText)

ShellExecuteEx( , 'runas', cFileRun, cRunParam, , SW_HIDE )
Do While .T.
   hb_IdleSleep( 1 )  // Gives time to run and then deletes the file
   Delete File(cRunParam)
   If !File(cRunParam)
      Exit
   Endif
Enddo
Return Nil

Function ProcedureOnRelease()
LOCAL cRegKey:=If(hexRegKey=0,"0","47")
	
If !(hexRegKey==hexRegKeyOld)
   *Win_RegWrite("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AeDebug\UserDebuggerHotKey",hexRegKey)
   SetRegValueAdmin(cRegKey)
   MsgExclamation("You will need to reboot this PC to takes effects.","Boot required")
Endif
Return Nil

#pragma BEGINDUMP

#include "SET_COMPILE_HMG_UNICODE.ch"
#include "HMG_UNICODE.h"

#include <windows.h>
#include <hbapi.h>
#include <shlobj.h>

HB_FUNC( SHELLEXECUTEEX )
{
    SHELLEXECUTEINFO SHExecInfo;
    ZeroMemory(&SHExecInfo, sizeof(SHExecInfo));

    SHExecInfo.cbSize       = sizeof(SHExecInfo);
    SHExecInfo.fMask        = SEE_MASK_NOCLOSEPROCESS;
    SHExecInfo.hwnd         = HB_ISNIL( 1 ) ? GetActiveWindow() : (HWND) HMG_parnl( 1 );
    SHExecInfo.lpVerb       = (LPCSTR) HMG_parc( 2 );
    SHExecInfo.lpFile       = (LPCSTR) HMG_parc( 3 );
    SHExecInfo.lpParameters = (LPCSTR) HMG_parc( 4 );
    SHExecInfo.lpDirectory  = (LPCSTR) HMG_parc( 5 );
    SHExecInfo.nShow        = hb_parni( 6 );
    
    if( ShellExecuteEx(&SHExecInfo) )
    {
        // hb_retnl( (LONG) SHExecInfo.hProcess );
        CloseHandle(SHExecInfo.hProcess);
    }
}

#pragma ENDDUMP
However, when you change the UserDebuggerHotKey key in the registry, you disable AeDebug (This is not a big loss for common user because it is used in execeptional and high level of MSDN development, I guessed). And it is critical that the machine must be rebooted after changing the registry for taking effect in the change.

This following captured screen was taken after boot. It Works !! :P
 
Screen123.png
Screen123.png (15.33 KiB) Viewed 4491 times
 
There is a interesting point that according MSDN saying that RegisterHotKey function can returns a value when is succeeds or not...
 
Screen121.png
Screen121.png (104.71 KiB) Viewed 4500 times
 
I think if our _DefineHotKey can be able to return value if success or not, it would helps. :?
 
Screen122.png
Screen122.png (10.42 KiB) Viewed 4496 times
 
I do not know in C if is this correct ?
Last edited by Pablo César on Mon Mar 13, 2017 10:06 am, edited 1 time in total.
HMGing a better world
"Matter tells space how to curve, space tells matter how to move."
Albert Einstein
KDJ
Posts: 243
Joined: Mon Sep 05, 2016 3:04 am
Location: Poland

Re: ON KEY F12 does not work in HMG 3.3.1

Post by KDJ »

Pablo

Oh yes, it works!
Thank you very much.

It is a little uncomfortable because you have to restart your machine.

Pablo César wrote: Sun Mar 12, 2017 7:20 pm I do not know in C if is this correct ?
I think in this way will be better:

Code: Select all

HB_FUNC ( INITHOTKEY )
{
  hb_retl(RegisterHotKey(
            (HWND) HMG_parnl(1), // window to receive hot-key notification
            hb_parni(4),         // identifier of hot key
            hb_parni(2),         // key-modifier flags
            hb_parni(3)          // virtual-key code
         ));
}
User avatar
Pablo César
Posts: 4059
Joined: Wed Sep 08, 2010 1:18 pm
Location: Curitiba - Brasil

ON KEY F12 does not work in HMG

Post by Pablo César »

KDJ wrote: Sun Mar 12, 2017 8:53 pm It is a little uncomfortable because you have to restart your machine.
Yes: it is uncomfortable to have to boot at least once. But also if there is so much need to enable this particular F12 key (taking into account so many others and their combinations) that perhaps it is worth doing this.

It's only one time at least that is needing to boot ...
KDJ wrote: Sun Mar 12, 2017 8:53 pmI think in this way will be better:

Code: Select all

HB_FUNC ( INITHOTKEY )
{
  hb_retl(RegisterHotKey(
            (HWND) HMG_parnl(1), // window to receive hot-key notification
            hb_parni(4),         // identifier of hot key
            hb_parni(2),         // key-modifier flags
            hb_parni(3)          // virtual-key code
         ));
}
Thank you Krzysztof. Now in my sample, it is working perfectly and with unsure when is able to register or when not.

I think _DefineHotKey in HMG could it be done like that. It's good to know the return value, specially for this case.

Let down the disposal the demo already improved:
 
HotKeys_F12.rar
Executable and source files demo
(1.24 MiB) Downloaded 223 times
 
After this our experience, we can say that the fault of not working, can not be attributed to HMG, but to MSDN. I think they should not take control of this key without the consent of the user. Do not you think ?
Last edited by Pablo César on Mon Mar 13, 2017 10:06 am, edited 2 times in total.
HMGing a better world
"Matter tells space how to curve, space tells matter how to move."
Albert Einstein
KDJ
Posts: 243
Joined: Mon Sep 05, 2016 3:04 am
Location: Poland

Re: ON KEY F12 does not work in HMG 3.3.1

Post by KDJ »

Yes Pablo, you are absolutely right.
Post Reply