Win32文本框控件alt按键消息

时间:2013-03-10 14:44:39

标签: winapi unicode keyboard editbox

我需要在子窗口控件中连接alt + alphanumberic keypress。

我编写了这个程序来测试它的功能。但它似乎没有响应击键 像我的应用程序(键盘翻译器)所需的ALT + X,ALT + X.

#include <windows.h>
#include <stdio.h>
#include <wchar.h>
#include <tchar.h>
#include <string.h>

#ifndef NULL
#define NULL 0
#endif

/* glaobal variables */
HWND hwndEdit = NULL;
HWND hwndWindow = NULL;
WCHAR* lpszClassName  = L"Vijesekara Keyboard Test Application";
WCHAR* lpszAppName = L"Vijesekara Keyboard Test Application";
int main_window_width = 0;
int main_window_height = 0;
WNDPROC edit_old_wndproc;

/*
  GWL_WNDPROC
*/
#ifndef GWL_WNDPROC 
#define GWL_WNDPROC (-4)
#endif

/* child window ID's */
#ifndef __CHILD_WINDOW_EDIT_ID__
#define __CHILD_WINDOW_EDIT_ID__
#define ID_EDIT     2000
// :TODO: more comes from here //
#endif 


/* constrains on child window positions */
#ifndef __CHILD_WINDOW_POSITION__
#define __CHILD_WINDOW_POSITION__
#define EDIT_CHILD_LEFT_PRECENTAGE 0.05
#define EDIT_CHILD_RIGHT_PRECENTAGE 0.05
#define EDIT_CHILD_TOP_PRECENTAGE 0.10
#define EDIT_CHILD_BOTTOM_PRECENTAGE 0.10
// :TODO: Other controls //
#endif

/* main window procedure */
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam,LPARAM lParam);
/* edit box procedure */
LRESULT CALLBACK EditBoxProc(HWND ,UINT , WPARAM , LPARAM );

/* resize child windows */
void ResizeChildWindows();

/* win main entry */
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    PSTR szCmdLine, int iCmdShow)
{

  WNDCLASS wndclass;

  wndclass.style = CS_HREDRAW |CS_VREDRAW ;
  wndclass.lpfnWndProc = WndProc;
  wndclass.cbClsExtra = 0;
  wndclass.hInstance = hInstance ;
  wndclass.hIcon = (HICON)LoadIcon(NULL,IDI_APPLICATION);
  wndclass.hCursor = (HCURSOR)LoadCursor(NULL,IDC_ARROW);
  wndclass.hbrBackground =(HBRUSH) GetStockObject( WHITE_BRUSH );
  wndclass.lpszMenuName = NULL ;
  wndclass.lpszClassName = lpszClassName ;

  /* Register class */
  int result ;
  result = RegisterClass( &wndclass) ;

  /* if failed to register window */
  if ( 0 == result )
  {
    MessageBox(NULL,L"Registering Window Have Been Failed" , lpszAppName, MB_OK);
    exit(0);  
  }

  /* Create the window */
  hwndWindow = CreateWindow(lpszAppName , lpszAppName ,WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,\
                 CW_USEDEFAULT, CW_USEDEFAULT,NULL,NULL,hInstance,NULL);
  if(NULL == hwndWindow )
  {
    MessageBox(NULL,L"Window Creation have being failed", lpszAppName,MB_OK);
    exit(0); 
  }              
  ShowWindow(hwndWindow, iCmdShow);

  /* Enter message loop */
  MSG msg ;
  while(GetMessage(&msg,NULL, 0,0 ))
  {
    TranslateMessage(&msg);
    DispatchMessage(&msg); 
  } 
  return msg.wParam;
}

/* window procedure */
LRESULT CALLBACK WndProc (HWND hwnd, UINT message,WPARAM wParam,LPARAM lParam)
{
  switch(message)
  {
    case WM_CREATE:
     if( hwndEdit != NULL)
     {
        MessageBox(NULL,L"Edit control handle have already initialized",lpszAppName , MB_OK);
        exit(0);
     } 
     hwndEdit = CreateWindow( L"edit" ,NULL, WS_CHILD|WS_VISIBLE|WS_VSCROLL\
                              |WS_BORDER|ES_LEFT|ES_MULTILINE|ES_AUTOVSCROLL,0,0,0,0,hwnd,(HMENU)ID_EDIT\
                              ,GetModuleHandle(NULL),NULL);
     if( NULL == hwndEdit )
     {
       MessageBox(hwnd, L"Edit child window control creation failed", lpszAppName, MB_OK);
       exit(0);
     }   

    edit_old_wndproc = (WNDPROC)SetWindowLongPtr(hwndEdit,GWL_WNDPROC,(LONG_PTR)EditBoxProc );
      break;

    case WM_SIZE:
       main_window_height = HIWORD(lParam);
       main_window_width = LOWORD (lParam);
       ResizeChildWindows();
       return 0;
    case WM_COMMAND:

      /*:TODO:process */
      break; 

    case WM_DESTROY:

       PostQuitMessage(0);
       return 0; 
    default:
      return DefWindowProc(hwnd,message,wParam,lParam);
  }
  return DefWindowProc(hwnd,message,wParam,lParam);
}

/* resize window procedure */
void ResizeChildWindows()
{
  MoveWindow(hwndEdit,main_window_width*EDIT_CHILD_LEFT_PRECENTAGE , main_window_height* EDIT_CHILD_TOP_PRECENTAGE , \
    main_window_width * (1- EDIT_CHILD_LEFT_PRECENTAGE - EDIT_CHILD_RIGHT_PRECENTAGE)          ,// height 
    main_window_height * (1- EDIT_CHILD_TOP_PRECENTAGE - EDIT_CHILD_BOTTOM_PRECENTAGE), TRUE   // width 
 );  
}
static int alt=0;
static int ctl=0;
static int sys =0;
WCHAR buffer[1024];

LRESULT CALLBACK EditBoxProc(HWND hwnd, UINT message, WPARAM wParam , LPARAM lParam)
{
  switch ( message)
  {

    case WM_SYSCHAR:
      sys =1;
      if( lParam && (1<<29) ) { alt= 1;}
      else { 
        alt=0;
      }

    case WM_KEYPRESS:
      wsprintf( buffer,TEXT("KeyCode is :%d"), wParam);
      MessageBox( NULL,buffer, TEXT("Vijesekara Keyboard"), MB_OK);
      SetFocus(hwnd);
      /* keyboard procedure */


    alt =0;
    if ( sys==1)
    {
      sys=0;
      break;
      }

    return 0;

    default:
      break;
  }
  return CallWindowProc(edit_old_wndproc ,hwndEdit,message,wParam,
           lParam);
}


// end of vijesekara_keyboard.cpp

注意:在子类文本框内,WM_CHAR消息不适用于alt键击,我确实如此 没有发送带有alt键的WM_CHAR消息,用于AlT +键盘。所以我使用spy ++记录所有消息 然后我发现WM_SYSCHAR已被发送而不是WM_CHAR用于编辑框子项内的那些消息 窗口。

任何想法我怎么能这样做?

- 提前致谢 -

1 个答案:

答案 0 :(得分:0)

好的,这很奇怪, 这是Alt + A键的间谍+输出。

enter image description here

然后我挂钩了WM_SYSKEYUP消息,而Fixup就是这个,

case WM_SYSKEYUP:
  if( lParam && (1<<29) ) {alt=1;}else{break;}
  if( wParam == 'A' || wParam=='Z'|| wParam =='X' || wParam== 'W' || wParam == 'Y' ){}else{break;}

case WM_SYSCHAR:
  sys =1;
  if( lParam && (1<<29) ) { alt=1; }
  else { 
    alt=0;
    break;
  }

 case WM_CHAR:
  wsprintf( buffer,TEXT("KeyCode is :%d"), wParam);
  MessageBox( NULL,buffer, TEXT("Vijesekara Keyboard"), MB_OK);
  SetFocus(hwnd);
  /* keyboard procedure */


  alt =0;


  return 0;

而且当我在WM_SYSKEYU上打破(调用默认窗口过程)时,它会发出令人讨厌的叮咚声 在Windows内部。我已经返回它,然后声音就消失了。

arx声称非常奇怪。是的我错了WM_KEYPRESS,没有一个名为WM_KEYPRESS的东西。 它应该更正为WM_CHAR。根据他没有这些机会MessageBox出现。那是 很奇怪。 @Arx,你可以发表你的答案,所以挖掘它的其他人可能会受益,而且 我也可以给你一些学分。并且还要挖掘为什么会发生这种情况,之后我们就可以接受 而这个答案将作为一个替代答案(不解决问题,但已经修复了 应用程序)。