![](/img/trans.png)
[英]How i can implement a IsKeyPressed function in a delphi console application?
[英]How can one verify whether consecutive letters have been entered from the qwerty keyboard in a delphi console application?
基本上,我有一個輸入,我想驗證用戶輸入是否使用了 qwerty 鍵盤布局中的 3 個或更多連續字母。 我的意思是 QWE 或 YUIOP。 首先,我將用戶輸入存儲在一個字符串變量中,並使用 ansiLowerCase 函數將輸入轉換為小寫。 我將 qwerty 布局聲明為常量字符串並使用 strscan 函數,但無濟於事。 任何幫助將不勝感激,謝謝。
嘗試這樣的事情:
function HasThreeConsecutiveLetters(const Str: string): Boolean;
const
QwertyLetters: array[0..2] of string = (
'QWERTYUIOP',
'ASDFGHJKL',
'ZXCVBNM'
);
var
I, J, K: Integer;
S: String;
begin
Result := False;
S := AnsiUpperCase(Str);
for I := 1 to Length(S) do
begin
for J := Low(QwertyLetters) to High(QwertyLetters) do
begin
K := Pos(S[I], QwertyLetters[J]);
if (K <> 0) and
((K+2) <= Length(QwertyLetters[J])) and
(Copy(S, I, 3) = Copy(QwertyLetters[J], K, 3)) then
begin
Result := True;
Exit;
end;
end;
end;
end;
然后你可以這樣做:
var
input: string;
begin
input := ...;
if HasThreeConsecutiveLetters(input) then
...
else
...
end;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.