0

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

我编写了这个程序来测试它的功能。但它似乎对我的应用程序(键盘翻译器)所必需的 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 击键,我没有发送 WM_CHAR 消息并按下 alt 键到 AlT+keystokes。所以我使用 spy++ 记录所有消息,然后我找到了 WM_SYSCHAR对于编辑框子窗口内的这些消息,已发送而不是 WM_CHAR。

知道我该怎么做吗?

- 提前致谢 -

4

1 回答 1

0

好吧,这很奇怪,这是我的 Alt+A 键的 spy+ 输出。

在此处输入图像描述

那么我已经挂钩了 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,您可以发布您的答案,以便挖掘它的其他人可能会受益,我也可以给您一些积分。并且还要挖掘为什么会发生这种情况,之后我们可以接受,这个答案将作为替代答案保留(不解决问题,但已经修复了应用程序)。

于 2013-03-10T18:57:04.040 回答