Problem with reading the Shift, Alt and Control keys in the console window.

General Help regarding HMG, Compilation, Linking, Samples

Moderator: Rathinagiri

Post Reply
edk
Posts: 911
Joined: Thu Oct 16, 2014 11:35 am
Location: Poland

Problem with reading the Shift, Alt and Control keys in the console window.

Post by edk »

Dear colleagues.
I have a problem with reading the status of the Shift, Alt and Control keys in the console window, in case we use the Msg commands (MsgInfo, MsgDebug, etc.) in the code.
Below is a simple example:

Code: Select all

#include <hmg.ch>

PROCEDURE MAIN
REQUEST HB_GT_WIN_DEFAULT

cons_hwnd := GETCONSOLEWINDOW()

SetMode( 25, 80 )
CLS

MsgInfo( "Hello" )	// <-------- Doesn't work with any Msg* :((( Why ?

SetForegroundWindow( cons_hwnd )
SetFocus( cons_hwnd )

DO WHILE .T.
	inkey(.1)
	@ 2,2 SAY "Alt Key Status         " + hb_valToStr( GetKeyState( VK_MENU ) )
	@ 3,2 SAY "Shift Key Status       " + hb_valToStr( GetKeyState( VK_SHIFT ) )
	@ 4,2 SAY "Ctrl Key Status        " + hb_valToStr( GetKeyState( VK_CONTROL ) )
	
	@ 6,2 SAY "Left Alt Key Status    " + hb_valToStr( GetKeyState( VK_LMENU ) )
	@ 7,2 SAY "Left Shift Key Status  " + hb_valToStr( GetKeyState( VK_LSHIFT ) )
	@ 8,2 SAY "Left Ctrl Key Status   " + hb_valToStr( GetKeyState( VK_LCONTROL ) )

	@10,2 SAY "Right Alt Key Status   " + hb_valToStr( GetKeyState( VK_RMENU ) )
	@11,2 SAY "Right Shift Key Status " + hb_valToStr( GetKeyState( VK_RSHIFT ) )
	@12,2 SAY "Right Ctrl Key Status  " + hb_valToStr( GetKeyState( VK_RCONTROL ) )

	@14,2 SAY SPACE ( 20 )
	@14,2 SAY IF ( isBit( KbdStat(), 1 ), "Shift ", "") + IF ( isBit( KbdStat(), 3 ), "Ctrl ", "") + IF ( isBit( KbdStat(), 4 ), "Alt", "")

	IF LastKey() = 27
		Exit
	End
EndDo
RETURN

/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/
#pragma BEGINDUMP

#include "hbapi.h"
#include "hbapiitm.h"
#include <windows.h>

// doesn't work with win32 function GetConsoleWindow()
HWND GetConWin()
{
HWND hwnd;
AllocConsole();
hwnd = FindWindowA("ConsoleWindowClass",NULL);
return hwnd;
}

HB_FUNC( GETCONSOLEWINDOW )
{
hb_retnl((long) GetConWin() );
}

HB_FUNC(HIDECONSOLE )
{
long h = hb_parnl(1);
if (h==NULL)
	h = GetConWin();
ShowWindow(h,SW_HIDE); // SW_HIDE
}

HB_FUNC(SHOWCONSOLE )
{
long h = hb_parnl(1);
if (h==NULL)
	h = GetConWin();
ShowWindow(h,SW_SHOW); // because 1'st time console stays minimized
ShowWindow(h,SW_RESTORE); //SW_SHOW
SetFocus(h);
SetForegroundWindow(h);
}

#pragma ENDDUMP
/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/
If you remove MsgInfo command then it works correctly.
Any ideas :?:
User avatar
serge_girard
Posts: 3165
Joined: Sun Nov 25, 2012 2:44 pm
DBs Used: 1 MySQL - MariaDB
2 DBF
Location: Belgium
Contact:

Re: Problem with reading the Shift, Alt and Control keys in the console window.

Post by serge_girard »

Really no idea!
Perhaps Claudio?
Serge
There's nothing you can do that can't be done...
User avatar
andyglezl
Posts: 1461
Joined: Fri Oct 26, 2012 7:58 pm
Location: Guadalajara Jalisco, MX
Contact:

Re: Problem with reading the Shift, Alt and Control keys in the console window.

Post by andyglezl »

DO EVENTS within the WHILE ...
The reason, I do not know.
Andrés González López
Desde Guadalajara, Jalisco. México.
edk
Posts: 911
Joined: Thu Oct 16, 2014 11:35 am
Location: Poland

Re: Problem with reading the Shift, Alt and Control keys in the console window.

Post by edk »

Many thanks Andy for the tip. :idea:
It is sufficient once DO EVENTS after MsgInfo.
Post Reply