【delphi】关于窗口接收键盘按键的问题,求指导~
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了【delphi】关于窗口接收键盘按键的问题,求指导~相关的知识,希望对你有一定的参考价值。
窗口的KeyPreview设置为true,窗口上有若干按钮。程序启动时,窗口能响应键盘按键消息,但是如果点击过窗口上的按钮以后(按钮为选中状态,等于按钮的default为true时的效果),这是窗口就不能响应键盘按键了,请问如何处理啊!
感觉原因就是,点击按钮以后,焦点停留在按钮上的原因,设置添加form.setFocus,无效。
.......
按照sirouszlh的提议安装了HotKeySpy控件,我只在子窗口中使用了该控件,但是我关闭子窗口后回到出窗口,设置的热键还在起作用,请问怎么处理呢?
虽然有bug 但基本可以用
我自己就修改成适合自己的追问
安装了HotKeySpy控件,我只在子窗口中使用了该控件,但是我关闭子窗口后回到出窗口,设置的热键还在起作用,请问怎么处理呢?
追答退出时,设置HotKeySpy1.HotKeys[0].Enabled:=false;
参考技术A 搜索一下delphi 键盘 钩子追问试了,按网上的例子编译没有通过呀
《Windows程序设计》读书笔六 键盘
6.1 键盘基础
6.1.1 忽略键盘
6.1.2 谁获得了焦点?
窗口过程通过WM_SETFOCUS和WM_KILLFOCUS消息来确定自己的窗口是否具有输入焦点
6.1.3 队列和同步
6.1.4 击键和字符
应用程序从windows接受的关于键盘事件的消息可分为击键和字符两种。
有些按键参数击键消息和字符消息
有些按键只产生击键消息
6.2 击键消息
6.2.1 系统键击和非系统键击
应用程序通常忽略WM_SYSKEYUP 和WM_SYSKEYDOWN消息,将他交给DefWindowProc来处理
如果你捕捉了也要再交给DefWindowProc处理
不与Alt组合会产生WM_KEYDOWN和WM_KEYUP消息。
6.2.2 虚拟键代码
虚拟键代码存储在WM_KEYDOWN, WM_KEYUP, WM_SYSKEYDOWN, WM_SYSKEYUP 的wParam中。
以VK开头,定义在winuser.h文件中
还有许多按键参考P179页
6.2.3 lparam信息
重复计数
OEM扫描码
扩展键标记
内容代码
键的先前状态
转换状态
6.2.4 转义状态
shift, Ctrl Alt 或者CapsLock NumLock Scroll Lock
iState = GetKeyState(VK_SHIFT); //如果shift被按下 iState为负,高位置1
iState = GetKeyState(VK_CAPITAL); //如果CapsLock按键打开,低位置1.
也可以获得VK_LBUTTON, VK_RBUTTON, VK_MBUTTON得到鼠标按钮的状态。
GetKeyState 无法获得功能键的状态
可以使用
GetAsyncKeyState
6.2.5 使用击键消息
windows通常不产生字符的击键使用WM_KEYDOWN消息,尽管可以通过击键消息和转义状态信息把击键转换为字符。你会在非英语键盘上遇到问题
对于光标移动,功能键,Insert Delete , WM_KEYDOWN 消息是最有用的。 但是Insert, Delete 经常被用作菜单快捷键。因为Windows会把菜单快捷键转换为菜单命令消息,所以应用程序不必自己处理这些击键。
总之大部分时间,你仅需要处理光标移动的WM_KEYDOWN 消息,有时候处理Insert Delete 的WM_KEYDOWN消息。当使用这些键时,可以通过GetKeyState函数来检查Shift和Ctrl犍的状态。
6.2.6 为SYSMETS加上键盘处理功能
system.h 代码
参见第四章
system.cpp 加入键盘控制的代码主要是把相应的键盘操作映射为相应的ScrollBar消息,简化代码。
#include <windows.h>
#include "sysmets.h"
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); //window procedure.
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT("SysMets4");
HWND hwnd;
MSG msg;
WNDCLASS wndClass; //The window Class
wndClass.style = CS_HREDRAW | CS_VREDRAW;
wndClass.lpfnWndProc = WndProc;// assign the window procedure to windows class.
wndClass.cbClsExtra = 0;
wndClass.cbWndExtra = 0;
wndClass.hInstance = hInstance;
wndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndClass.lpszMenuName = NULL;
wndClass.lpszClassName = szAppName;
//Register the Window Class to the Windows System.
if (!RegisterClass(&wndClass))
{
MessageBox(NULL, TEXT("This program require Windows NT!"),
szAppName, MB_ICONERROR);
return 0;
}
//This function will generate an WM_CREATE message.
hwnd = CreateWindow(szAppName, //Window class name
TEXT("Get System Metrics No. 4"), //Window caption
WS_OVERLAPPEDWINDOW | WS_VSCROLL | WS_HSCROLL, //Window Style
CW_USEDEFAULT, //initial x position
CW_USEDEFAULT, //initial y position
CW_USEDEFAULT, //initial x size
CW_USEDEFAULT, //initial y size
NULL, //parent window handle
NULL, //window menu handle
hInstance, //program instance handle
NULL); //creation parameters
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd); //This function will generate a WM_PAINT message.
/* The message loop for this program.
if received the WM_QUIT message, the function will return 0.*/
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
//define the Window Procedure WndProc
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static int cxChar, cxCaps, cyChar, cxClient, cyClient, iMaxWidth;
HDC hdc;
int i, x, y, iVertPos, iHorzPos, iPaintBeg, iPaintEnd;
PAINTSTRUCT ps;
SCROLLINFO si;
TCHAR szBuffer[10];
TEXTMETRIC tm;
switch (message) //get the message
{
case WM_CREATE:
hdc = GetDC(hwnd);
GetTextMetrics(hdc, &tm);
cxChar = tm.tmAveCharWidth;
cxCaps = (tm.tmPitchAndFamily & 1 ? 3 : 2)* cxChar / 2;
cyChar = tm.tmHeight + tm.tmExternalLeading;
ReleaseDC(hwnd, hdc);
iMaxWidth = 40 * cxChar + 22 * cxCaps;
return 0;
case WM_SIZE:
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
//Set vertical scroll bar range and page size
si.cbSize = sizeof(si);
si.fMask = SIF_RANGE | SIF_PAGE;
si.nMin = 0;
si.nMax = NUMLINES - 1;
si.nPage = cyClient / cyChar;
SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
//Set the horizontal scroll bar range and page size
si.cbSize = sizeof(si);
si.fMask = SIF_RANGE | SIF_PAGE;
si.nMin = 0;
si.nMax = 2 + iMaxWidth / cxChar;
si.nPage = cxClient / cxChar;
SetScrollInfo(hwnd, SB_HORZ, &si, TRUE);
return 0;
case WM_VSCROLL:
//Get all the vertical scroll bar information
si.cbSize = sizeof(si);
si.fMask = SIF_ALL;
GetScrollInfo(hwnd, SB_VERT, &si);
//Save the position for comparison later on
iVertPos = si.nPos;
switch (LOWORD(wParam))
{
case SB_TOP:
si.nPos = si.nMin;
break;
case SB_BOTTOM:
si.nPos = si.nMax;
break;
case SB_LINEUP:
si.nPos -= 1;
break;
case SB_LINEDOWN:
si.nPos += 1;
break;
case SB_PAGEUP:
si.nPos -= si.nPage;
break;
case SB_PAGEDOWN:
si.nPos += si.nPage;
break;
case SB_THUMBPOSITION:
si.nPos = si.nTrackPos;
break;
default:
break;
}
//Set the position and then retrieve it. Due to adjustments
//By Windows it may not be the same as the value set.
si.fMask = SIF_POS;
SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
GetScrollInfo(hwnd, SB_VERT, &si);
// if the position has changed, scroll the window and update it
if (si.nPos != iVertPos)
{
ScrollWindow(hwnd, 0, cyChar*(iVertPos - si.nPos),
NULL, NULL);
UpdateWindow(hwnd); //instead of the invalidateRect() it will update the rect immediately.
}
return 0;
case WM_HSCROLL:
// Get all the vertical scroll bar information
si.cbSize = sizeof(si);
si.fMask = SIF_ALL;
//Save the position for comparison later on
GetScrollInfo(hwnd, SB_HORZ, &si);
iHorzPos = si.nPos;
switch (LOWORD(wParam))
{
case SB_LINELEFT:
si.nPos -= 1;
break;
case SB_LINERIGHT:
si.nPos += 1;
break;
case SB_PAGELEFT:
si.nPos -= si.nPage;
break;
case SB_PAGERIGHT:
si.nPos += si.nPage;
break;
case SB_THUMBPOSITION:
si.nPos = si.nTrackPos;
break;
default:
break;
}
//Set the Position and then retrieve it. Due to adjustments
//by Windows it may not be the same as the value set.
si.fMask = SIF_POS;
SetScrollInfo(hwnd, SB_HORZ, &si, TRUE);
GetScrollInfo(hwnd, SB_HORZ, &si);
//If the position has been changed, scroll the window
if (si.nPos != iHorzPos)
{
ScrollWindow(hwnd, cxChar* (iHorzPos - si.nPos), 0,
NULL, NULL);
}
return 0;
case WM_KEYDOWN:
switch(wParam)
{
case VK_HOME:
SendMessage(hwnd, WM_VSCROLL, SB_TOP, 0);
break;
case VK_END:
SendMessage(hwnd, WM_VSCROLL, SB_BOTTOM, 0);
break;
case VK_PRIOR:
SendMessage(hwnd, WM_VSCROLL, SB_PAGEUP, 0);
break;
case VK_NEXT:
SendMessage(hwnd, WM_VSCROLL, SB_PAGEDOWN, 0);
break;
case VK_UP:
SendMessage(hwnd, WM_VSCROLL, SB_LINEUP, 0);
break;
case VK_DOWN:
SendMessage(hwnd, WM_VSCROLL, SB_LINEDOWN, 0);
break;
case VK_LEFT:
SendMessage(hwnd, WM_HSCROLL, SB_PAGEUP, 0);
break;
case VK_RIGHT:
SendMessage(hwnd, WM_HSCROLL, SB_PAGEDOWN, 0);
break;
}
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
//Get the vertical scroll bar position
si.cbSize = sizeof(si);;
si.fMask = SIF_POS;
GetScrollInfo(hwnd, SB_VERT, &si);
iVertPos = si.nPos;
//Get horizontal scroll bar position
GetScrollInfo(hwnd, SB_HORZ, &si);
iHorzPos = si.nPos;
//Find painting limits
iPaintBeg = max(0, iVertPos + ps.rcPaint.top / cyChar);
iPaintEnd = min(NUMLINES - 1,
iVertPos + ps.rcPaint.bottom / cyChar);
for (i = iPaintBeg; i <= iPaintEnd; ++i)
{
x = cxChar * (1 - iHorzPos);
y = cyChar * (i - iVertPos);
TextOut(hdc, x, y,
sysmetrics[i].szLabel,
lstrlen(sysmetrics[i].szLabel));
TextOut(hdc, x + 22 * cxCaps, y,
sysmetrics[i].szDesc,
lstrlen(sysmetrics[i].szDesc));
SetTextAlign(hdc, TA_RIGHT | TA_TOP);
TextOut(hdc, x + 22 * cxCaps + 40 * cxChar, y, szBuffer,
wsprintf(szBuffer, TEXT("%5d"),
GetSystemMetrics(sysmetrics[i].iIndex)));
SetTextAlign(hdc, TA_LEFT | TA_TOP);
}
EndPaint(hwnd, &ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
执行结果如下
6.3 字符消息
在消息循环中 TranslateMessage函数,负责把击键消息转换为字符消息。 WM_KEYDOWN WM_SYSKEYDOWN.
6.3.1 四类字符消息