Hi Jose,
Using hooks is a hack that is not generally advisable to use. If you have the option, switch over to a UI to take that input which you are trying to mask. That way you will not need to use hooks.
However if you still need to go this route, then here is a code snippet based on the low level keyboard hooks. Hope this helps. I havent tested it very extensively but seems to work in general using AutoCAD 2013.
Here is the sample code :
static ACHAR password[133];
static int pwdLen;
static HHOOK hKeyboardHook;
// - AdskMyTest1.MyCommand1 command (do not rename)
static void AdskTestCommand(void)
{
ACHAR pwd[133];
adskGetPassword(1, ACRX_T("Password please ?"), pwd);
}
static int adskGetPassword(int cronly,ACHAR* prompt,ACHAR* result)
{
pwdLen = 0;
password[pwdLen]='\0';
HINSTANCE hInstance = GetModuleHandle(NULL);
if (!hInstance)
return 1;
hKeyboardHook = SetWindowsHookEx (
WH_KEYBOARD_LL,
(HOOKPROC) KeyboardEvent,
hInstance,
NULL
);
ACHAR tempResult[133];
int ret = acedGetString(cronly, prompt, tempResult);
UnhookWindowsHookEx(hKeyboardHook);
if(ret != RTNORM)
acutPrintf(ACRX_T("\nCancelled."));
else
{
password[++pwdLen] = '\0';
acutPrintf(password);
}
wcscpy(result, password);
return ret;
}
static LRESULT CALLBACK KeyboardEvent (int nCode, WPARAM wParam, LPARAM lParam)
{
if ((nCode == HC_ACTION) &&
((wParam == WM_SYSKEYDOWN) ||
(wParam == WM_KEYDOWN)))
{
KBDLLHOOKSTRUCT hooked_key = *((KBDLLHOOKSTRUCT*)lParam);
DWORD dwMsg = 1;
dwMsg += hooked_key.scanCode << 16;
dwMsg += hooked_key.flags << 24;
int key = hs.vkCode;
if ((key >= 'A' && key <= 'Z') || (key >= '0' && key <= '9'))
{
GetKeyNameText(dwMsg, &password[pwdLen], 0xFF) + 1;
password[++pwdLen] = '\0';
keybd_event(VK_MULTIPLY, hooked_key.scanCode, hooked_key.flags, 0);
return 1;
}
else if(hooked_key.vkCode == VK_BACK)
{
password[--pwdLen] = '\0';
}
}
return CallNextHookEx(hKeyboardHook, nCode,wParam,lParam);
}
Balaji
Developer Technical Services
Autodesk Developer Network