中国程序员联盟 正在重新改版中ing 不便之处还请见谅 改版后将内容涉及java delphi .net php
 
  首页 | 数据库开发 | 网络通讯 | 多线程 | 多媒体开发 | 图像处理 | 程序人生 | 系统函数 | 控件开发 | Web服务
 
  当前位置:笨鱼delphi技术网>系统函数>文章内容

delphi 关闭数字键盘

来源:国外 关于:Jonas Bilinkevicius 发布时间:2007-07-03   [收藏] [推荐]
Problem/Question/Abstract:
Using Delphi 5, I'm trying to setup a routine that would automatically turn off the NUMLOCK key when loaded. Assume that I am writing a standalone utility that could be loaded in the startup folder to do this function.
Answer:
Solve 1:
procedure SwitchToggleKey(Key: byte; State: boolean);
var
  ks: TKeyboardState;
  ScanCode: integer;
begin
  if not key in [VK_CAPITAL, VK_NUMLOCK, VK_SCROLL, VK_INSERT] then
    exit;
  if (key = VK_NUMLOCK) and (Win32Platform = VER_PLATFORM_WIN32_WINDOWS) then
  begin
    GetKeyboardState(ks); {for Win95/98}
    if state then
      ks[key] := ks[key] or 1
    else
      ks[key] := ks[key] and 254;
    SetKeyboardState(ks);
  end
  else if odd(GetKeyState(key)) <> state then
  begin
    ScanCode := MapVirtualKey(key, 0);
    keybd_event(key, ScanCode, {KEYEVENTF_EXTENDEDKEY} 0, 0);
    {Simulate a key release}
    keybd_event(key, ScanCode, {KEYEVENTF_EXTENDEDKEY or } KEYEVENTF_KEYUP, 0);
  end;
end;
Note that not all controls "honor" the INSERT key, and others will only respond to the INSERT key while they have focus. I'm surprised that the Extended Key "attribute" works for the non-extended keys. Strangely enough, it works as well without KEYEVENTF_EXTENDEDKEY.

Solve 2:
procedure SimulateKeystroke(Key: byte; extra: DWORD);
begin
  keybd_event(Key, extra, 0, 0);
  keybd_event(Key, extra, KEYEVENTF_KEYUP, 0);
end;
function IsKeyToggled(key: byte): boolean;
var
  state: word;
begin
  state := windows.GetKeyState(key);
  result := (state mod 128) = 1;
end;
function CapsLockStatus: boolean;
begin
  result := IsKeyToggled(VK_CAPITAL);
end;
function NumLockStatus: boolean;
begin
  result := IsKeyToggled(VK_NUMLOCK);
end;
procedure ToggleCapsLock;
begin
  SimulateKeystroke(VK_CAPITAL, 0);
end;
procedure ToggleNumLock;
begin
  SimulateKeystroke(VK_NUMLOCK, 0);
end;
procedure TForm1.btnOnClick(Sender: TObject);
begin
  if not NumLockStatus then
    ToggleNumLock;
end;
procedure TForm1.btnOffClick(Sender: TObject);
begin
  if NumLockStatus then
    ToggleNumLock;
end;

Solve 3:
I want to determine the state of the Num lock key on the keyboard and set it to on when my application begins or opens a specific form.
Note that the keyboard LED may not reflect the keys state correctly on all Windows platforms if you set it this way in code.
procedure SetLockKey(vcode: Integer; down: Boolean);
begin
  if Odd(GetAsyncKeyState(vcode)) <> down then
  begin
    keybd_event(vcode, MapVirtualkey(vcode, 0), KEYEVENTF_EXTENDEDKEY, 0);
    keybd_event(vcode, MapVirtualkey(vcode, 0), KEYEVENTF_EXTENDEDKEY or
      KEYEVENTF_KEYUP, 0);
  end;
end;
SetLockKey(VK_NUMLOCK, True); {num lock down}

[浏览: 次]   
上一篇:delphi 如何区分键盘两个Enter键   下一篇:delphi CapsLock打开与关闭
[收藏] [推荐] [返回顶部] [打印本页] [关闭窗口]  
    评论加载中…
google adsense热点文章
·delphi Delphi中ShellExecute的妙用
·delphi 如何快速读取文本文件
·delphi 如何判断输入值是否中文
·delphi 在应用层截获键盘消息
·delphi delphi实现服务开启与关闭
·delphi 实时记录事件日志
·delphi 使MEMO自动滚动
·delphi 如何区分键盘两个Enter键
·delphi 切换界面的方法
·delphi 汉字输入法的编程及使用
·delphi Delphi程序输入法自动切换最简
·delphi 程序缩小为任务右下角的小图标
     delphi技术网 | firefox 下载 | Avant Browser下载 | dedecms 技术网 | drupal 爱好者 | php 技术网
  Copyright@www.delphichm.com,2006-2009.All Rights Reserved.
 
程序员联盟 | delphi Java .net|QQ:707102932