如何在文本框里实现字符的输入显示?(50分)

  • 主题发起人 主题发起人 sunnysheng
  • 开始时间 开始时间
S

sunnysheng

Unregistered / Unconfirmed
GUEST, unregistred user!
我要在文本框里实现 Alt + 数字键显示字符,如: Alt + 255 ,在文本编辑框里显示FF,<br>Alt + 1 显示 01,Alt + 82 显示 R ,也就是说Alt + 字符的十进制码,即响应相应的ASCII(十六进制)码。
 
KeyDown中有一个参数是Shift,可以知道你按下了什么控制键
 
procedure TForm1.Edit1KeyUp(Sender: TObject; var Key: Word;<br>&nbsp; Shift: TShiftState);<br>{$J+} &nbsp;<br>const<br>&nbsp; s: string = '';<br>var<br>&nbsp; b: Byte;<br>begin<br>&nbsp; if Key=18 then<br>&nbsp; begin<br>&nbsp; &nbsp; try &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; b:=StrToInt(s)<br>&nbsp; &nbsp; except<br>&nbsp; &nbsp; &nbsp; b:=0<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; s:='';<br>&nbsp; &nbsp; TEdit(Sender).Text:=TEdit(Sender).Text+Chr(b)<br>&nbsp; end<br>&nbsp; else if (ssAlt in Shift) and (Key in [48..57]) then<br>&nbsp; &nbsp; s:=s+Chr(Key);<br>&nbsp; Key:=0<br>end;<br>
 
unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br>&nbsp; Db, ADODB, StdCtrls;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Edit1: TEdit;<br>&nbsp; &nbsp; Edit2: TEdit;<br>&nbsp; &nbsp; procedure Edit1KeyUp(Sender: TObject; var Key: Word;<br>&nbsp; &nbsp; &nbsp; Shift: TShiftState);<br>&nbsp; &nbsp; procedure FormCreate(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br><br>implementation<br><br>{$R *.DFM}<br><br>procedure TForm1.Edit1KeyUp(Sender: TObject; var Key: Word;<br>&nbsp; Shift: TShiftState);<br>begin<br>&nbsp; IF (Shift &lt;&gt;[ssAlt])or (not (Key in['0'..'9'])) then begin<br>&nbsp; Key :=ord(#0);<br>&nbsp; exit;<br>&nbsp; end;<br>&nbsp; Edit2.Text := inttoHex(strtoint(Edit1.Text),4);<br><br>end;<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br>&nbsp; Edit1.Text := '';<br>end;<br><br>end.<br>
 
procedure TForm1.Edit1KeyUp(Sender: TObject; var Key: Word;<br>&nbsp; Shift: TShiftState);<br><br>if Shift=[ssAlt] then showmessage('按下了alt键'); <br>&nbsp; 而key就是 另外一个普通的键。。。。 &nbsp;
 
顶LeeChange
 
后退
顶部