Alternative zu KeyboardInkey() - dt. incl. öäü Sonderz. usw.

Hier könnt Ihr gute, von Euch geschriebene Codes posten. Sie müssen auf jeden Fall funktionieren und sollten möglichst effizient, elegant und beispielhaft oder einfach nur cool sein.
Benutzeravatar
Lebostein
Beiträge: 674
Registriert: 13.09.2004 11:31
Wohnort: Erzgebirge

Alternative zu KeyboardInkey() - dt. incl. öäü Sonderz. usw.

Beitrag von Lebostein »

Hallo, hier mal meine Variante zu KeyboardInkey(). Dieser Code ist auf die deutsche Tastatur zugeschnitten und enthält alle Zeichen auf der Tastatur (inkl. Shift- und AltGr-Kombinationen). Gegenüber KeyboardInkey erfolgt hier beim Tastendruck sofort eine Reaktion, nicht erst beim Loslassen der Taste. Außerdem ist eine 'BackSpace'-Abfrage sowie eine 'Löschen'-Abfrage bereits mit eingebaut:

Code: Alles auswählen

;**************************************************************
;* INCLUDE FOR PUREBASIC 3.92
;**************************************************************
;* Titel: Tastaturabfrage bei OpenScreen (detusche Tastatur)
;* Autor: Lebostein
;* Datum: 21.01.2005
;**************************************************************

;==============================================================
; Variablen und Konstanten der Include-Datei
;==============================================================

Structure KeySet
  keycode.l
  combine.l[3]
EndStructure

NewList KeyList.KeySet()
NewList PushedKey.l()

;==============================================================
; Gibt einmalig 1 zurück, wenn Taste niedergedrückt wird
;==============================================================

Procedure KeyboardTyping(code)

  If KeyboardPushed(code) = 0: ProcedureReturn: EndIf
  ForEach PushedKey(): If PushedKey() = code: exist = 1: EndIf: Next
  If exist = 0: AddElement(PushedKey()): PushedKey() = code: ProcedureReturn 1: EndIf

EndProcedure

;==============================================================
; Entspricht einem Inkey-Befehl
;==============================================================

Procedure.s KeyboardText(eingabe$)

  ForEach PushedKey(): If KeyboardPushed(PushedKey()) = 0: DeleteElement(PushedKey()): EndIf: Next

  If KeyboardPushed(#PB_Key_RightShift): combi = 1: EndIf
  If KeyboardPushed(#PB_Key_LeftShift) : combi = 1: EndIf
  If KeyboardPushed(#PB_Key_RightAlt)  : combi = 2: EndIf 

  ForEach KeyList()
  code = KeyList()\keycode
  wert = KeyList()\combine[combi]
  If KeyboardTyping(code): eingabe$ + Chr(wert): EndIf: Next
  If KeyboardTyping(#PB_Key_Back): eingabe$ = Left(eingabe$, Len(eingabe$) - 1): EndIf
  If KeyboardTyping(#PB_Key_Delete): eingabe$ = Space(0): EndIf
  
  ProcedureReturn eingabe$

EndProcedure

;==============================================================
; Einen Keycode hinzufügen
;==============================================================

Procedure AddKey(code.l, none_combi.l, with_shift.l, with_altgr.l)

  AddElement(KeyList())
  KeyList()\keycode = code
  KeyList()\combine[0] = none_combi
  KeyList()\combine[1] = with_shift
  KeyList()\combine[2] = with_altgr

EndProcedure

;==============================================================
; Alle Keycodes hinzufügen
;==============================================================

; Leertaste (Space)
AddKey(057, 032, 032, 000)    ; [ ] [ ] [ ] ... 057 = #PB_Key_Space

; Tasten im Hauptblock (48)
AddKey(002, 049, 033, 000)    ; [1] [!] [ ] ... 002 = #PB_Key_1
AddKey(003, 050, 034, 178)    ; [2] ["] [²] ... 003 = #PB_Key_2
AddKey(004, 051, 167, 179)    ; [3] [§] [³] ... 004 = #PB_Key_3
AddKey(005, 052, 036, 000)    ; [4] [$] [ ] ... 005 = #PB_Key_4
AddKey(006, 053, 037, 000)    ; [5] [%] [ ] ... 006 = #PB_Key_5
AddKey(007, 054, 038, 000)    ; [6] [&] [ ] ... 007 = #PB_Key_6
AddKey(008, 055, 047, 123)    ; [7] [/] [{] ... 008 = #PB_Key_7
AddKey(009, 056, 040, 091)    ; [8] [(] [[] ... 009 = #PB_Key_8
AddKey(010, 057, 041, 093)    ; [9] [)] []] ... 010 = #PB_Key_9
AddKey(011, 048, 061, 125)    ; [0] [=] [}] ... 011 = #PB_Key_0
AddKey(012, 223, 063, 092)    ; [ß] [?] [\] ... 012 = #PB_Key_Minus
AddKey(013, 180, 096, 000)    ; [´] [`] [ ] ... 013 = #PB_Key_Equals
AddKey(016, 113, 081, 064)    ; [q] [Q] [@] ... 016 = #PB_Key_Q
AddKey(017, 119, 087, 000)    ; [w] [W] [ ] ... 017 = #PB_Key_W
AddKey(018, 101, 069, 128)    ; [e] [E] [€] ... 018 = #PB_Key_E
AddKey(019, 114, 082, 000)    ; [r] [R] [ ] ... 019 = #PB_Key_R
AddKey(020, 116, 084, 000)    ; [t] [T] [ ] ... 020 = #PB_Key_T
AddKey(021, 122, 090, 000)    ; [z] [Z] [ ] ... 021 = #PB_Key_Y
AddKey(022, 117, 085, 000)    ; [u] [U] [ ] ... 022 = #PB_Key_U
AddKey(023, 105, 073, 000)    ; [i] [I] [ ] ... 023 = #PB_Key_I
AddKey(024, 111, 079, 000)    ; [o] [O] [ ] ... 024 = #PB_Key_O
AddKey(025, 112, 080, 000)    ; [p] [P] [ ] ... 025 = #PB_Key_P
AddKey(026, 252, 220, 000)    ; [ü] [Ü] [ ] ... 026 = #PB_Key_LeftBracket
AddKey(027, 043, 042, 126)    ; [+] [*] [~] ... 027 = #PB_Key_RightBracket
AddKey(030, 097, 065, 000)    ; [a] [A] [ ] ... 030 = #PB_Key_A
AddKey(031, 115, 083, 000)    ; [s] [S] [ ] ... 031 = #PB_Key_S
AddKey(032, 100, 068, 000)    ; [d] [D] [ ] ... 032 = #PB_Key_D
AddKey(033, 102, 070, 000)    ; [f] [F] [ ] ... 033 = #PB_Key_F
AddKey(034, 103, 071, 000)    ; [g] [G] [ ] ... 034 = #PB_Key_G
AddKey(035, 104, 072, 000)    ; [h] [H] [ ] ... 035 = #PB_Key_H
AddKey(036, 106, 074, 000)    ; [j] [J] [ ] ... 036 = #PB_Key_J
AddKey(037, 107, 075, 000)    ; [k] [K] [ ] ... 037 = #PB_Key_K
AddKey(038, 108, 076, 000)    ; [l] [L] [ ] ... 038 = #PB_Key_L
AddKey(039, 246, 214, 000)    ; [ö] [Ö] [ ] ... 039 = #PB_Key_SemiColon
AddKey(040, 228, 196, 000)    ; [ä] [Ä] [ ] ... 040 = #PB_Key_Apostrophe
AddKey(041, 094, 176, 000)    ; [^] [°] [ ] ... 041 = #PB_Key_Grave
AddKey(043, 035, 039, 000)    ; [#] ['] [ ] ... 043 = #PB_Key_BackSlash
AddKey(044, 121, 089, 000)    ; [y] [Y] [ ] ... 044 = #PB_Key_Z
AddKey(045, 120, 088, 000)    ; [x] [X] [ ] ... 045 = #PB_Key_X
AddKey(046, 099, 067, 000)    ; [c] [C] [ ] ... 046 = #PB_Key_C
AddKey(047, 118, 086, 000)    ; [v] [V] [ ] ... 047 = #PB_Key_V
AddKey(048, 098, 066, 000)    ; [b] [B] [ ] ... 048 = #PB_Key_B
AddKey(049, 110, 078, 000)    ; [n] [N] [ ] ... 049 = #PB_Key_N
AddKey(050, 109, 077, 181)    ; [m] [M] [µ] ... 050 = #PB_Key_M
AddKey(051, 044, 059, 000)    ; [,] [;] [ ] ... 051 = #PB_Key_Comma
AddKey(052, 046, 058, 000)    ; [.] [:] [ ] ... 052 = #PB_Key_Period
AddKey(053, 045, 095, 000)    ; [-] [_] [ ] ... 053 = #PB_Key_Slash
AddKey(086, 060, 062, 124)    ; [<] [>] [|] ... 086 = keine PB-Konstante

; Tasten auf dem Keypad (15)
AddKey(055, 042, 000, 000)    ; [*] [ ] [ ] ... 055 = #PB_Key_Multiply
AddKey(071, 055, 000, 000)    ; [7] [ ] [ ] ... 071 = #PB_Key_Pad7
AddKey(072, 056, 000, 000)    ; [8] [ ] [ ] ... 072 = #PB_Key_Pad8
AddKey(073, 057, 000, 000)    ; [9] [ ] [ ] ... 073 = #PB_Key_Pad9
AddKey(074, 045, 000, 000)    ; [-] [ ] [ ] ... 074 = #PB_Key_Subtract
AddKey(075, 052, 000, 000)    ; [4] [ ] [ ] ... 075 = #PB_Key_Pad4
AddKey(076, 053, 000, 000)    ; [5] [ ] [ ] ... 076 = #PB_Key_Pad5
AddKey(077, 054, 000, 000)    ; [6] [ ] [ ] ... 077 = #PB_Key_Pad6
AddKey(078, 043, 000, 000)    ; [+] [ ] [ ] ... 078 = #PB_Key_Add
AddKey(079, 049, 000, 000)    ; [1] [ ] [ ] ... 079 = #PB_Key_Pad1
AddKey(080, 050, 000, 000)    ; [2] [ ] [ ] ... 080 = #PB_Key_Pad2
AddKey(081, 051, 000, 000)    ; [3] [ ] [ ] ... 081 = #PB_Key_Pad3
AddKey(082, 048, 000, 000)    ; [0] [ ] [ ] ... 082 = #PB_Key_Pad0
AddKey(083, 044, 000, 000)    ; [,] [ ] [ ] ... 083 = #PB_Key_Decimal
AddKey(181, 047, 000, 000)    ; [/] [ ] [ ] ... 181 = #PB_Key_Divide

;==============================================================
; Ende der Include-Datei
;==============================================================



; -------------------------
; --- Beispielprogramm ----
; -------------------------

InitSprite()
InitKeyboard()
OpenScreen(800,600,16,"Eingabe")

Repeat

  ExamineKeyboard()
  
  text$ = KeyboardText(text$)
  ;text$ + KeyboardInkey()

  ClearScreen(0,0,0)
  StartDrawing(ScreenOutput())
    DrawingMode(1)
    Locate(10,100)
    FrontColor(100,255,0)
    DrawText(text$ + "_")
  StopDrawing()
  FlipBuffers()
 
Until KeyboardPushed(#PB_Key_Escape)
crX
Beiträge: 21
Registriert: 29.12.2004 17:29
Wohnort: Dort wo's leckcher Fondüü hat

Beitrag von crX »

hier das ganze für das Tastaturlayout sg (schwiizertüütsch *g*)

Code: Alles auswählen

;************************************************************** 
;* INCLUDE FOR PUREBASIC 3.92 
;************************************************************** 
;* Titel: Tastaturabfrage bei OpenScreen (Layout SG) 
;* Autor: Lebostein 
;* Datum: 21.01.2005 
;************************************************************** 

;============================================================== 
; Variablen und Konstanten der Include-Datei 
;============================================================== 

Structure KeySet 
  keycode.l 
  combine.l[3] 
EndStructure 

NewList KeyList.KeySet() 
NewList PushedKey.l() 

;============================================================== 
; Gibt einmalig 1 zurück, wenn Taste niedergedrückt wird 
;============================================================== 

Procedure KeyboardTyping(code) 
  
  If KeyboardPushed(code) = 0: ProcedureReturn: EndIf 
  ForEach PushedKey(): If PushedKey() = code: exist = 1: EndIf: Next 
  If exist = 0: AddElement(PushedKey()): PushedKey() = code: ProcedureReturn 1: EndIf 
  
EndProcedure 

;============================================================== 
; Entspricht einem Inkey-Befehl 
;============================================================== 

Procedure.s KeyboardText(eingabe$) 
  
  ForEach PushedKey(): If KeyboardPushed(PushedKey()) = 0: DeleteElement(PushedKey()): EndIf: Next 
  
  If KeyboardPushed(#PB_Key_RightShift): combi = 1: EndIf 
  If KeyboardPushed(#PB_Key_LeftShift) : combi = 1: EndIf 
  If KeyboardPushed(#PB_Key_RightAlt)  : combi = 2: EndIf 
  
  ForEach KeyList() 
    code = KeyList()\keycode 
    wert = KeyList()\combine[combi] 
  If KeyboardTyping(code): eingabe$ + Chr(wert): EndIf: Next 
  If KeyboardTyping(#PB_Key_Back): eingabe$ = Left(eingabe$, Len(eingabe$) - 1): EndIf 
  If KeyboardTyping(#PB_Key_Delete): eingabe$ = Space(0): EndIf 
  
  ProcedureReturn eingabe$ 
  
EndProcedure 

;============================================================== 
; Einen Keycode hinzufügen 
;============================================================== 

Procedure AddKey(code.l, none_combi.l, with_shift.l, with_altgr.l) 
  
  AddElement(KeyList()) 
  KeyList()\keycode = code 
  KeyList()\combine[0] = none_combi 
  KeyList()\combine[1] = with_shift 
  KeyList()\combine[2] = with_altgr 
  
EndProcedure 

;============================================================== 
; Alle Keycodes hinzufügen 
;============================================================== 

; Leertaste (Space) 
AddKey(057, 032, 032, 000)    ; [ ] [ ] [ ] ... 057 = #PB_Key_Space 

; Tasten im Hauptblock (48) 
AddKey(002,049,043,166) ;  ... [1] [+] [¦]
AddKey(003,050,034,064) ;  ... [2] ["] [@]
AddKey(004,051,042,035) ;  ... [3] [*] [#]
AddKey(005,052,231,000) ;  ... [4] [ç] []
AddKey(006,053,037,000) ;  ... [5] [%] []
AddKey(007,054,038,172) ;  ... [6] [&] [¬]
AddKey(008,055,047,124) ;  ... [7] [/] [|]
AddKey(009,056,040,162) ;  ... [8] [(] [¢]
AddKey(010,057,041,000) ;  ... [9] [)] []
AddKey(011,048,061,000) ;  ... [0] [=] []
AddKey(012,039,063,180) ;  ... ['] [?] [´]
AddKey(013,094,096,126) ;  ... [^] [`] [~]
AddKey(016,113,081,000) ;  ... [q] [Q] []
AddKey(017,119,087,000) ;  ... [w] [W] []
AddKey(018,101,069,128) ;  ... [e] [E] [€]
AddKey(019,114,082,000) ;  ... [r] [R] []
AddKey(020,116,084,000) ;  ... [t] [T] []
AddKey(021,122,090,000) ;  ... [z] [Z] []
AddKey(022,117,085,000) ;  ... [u] [U] []
AddKey(023,105,073,000) ;  ... [i] [I] []
AddKey(024,111,079,000) ;  ... [o] [O] []
AddKey(025,112,080,000) ;  ... [p] [P] []
AddKey(026,252,232,000) ;  ... [ü] [è] []
AddKey(027,168,033,000) ;  ... [¨] [!] []
AddKey(030,097,065,000) ;  ... [a] [A] []
AddKey(031,115,083,000) ;  ... [s] [S] []
AddKey(032,100,068,000) ;  ... [d] [D] []
AddKey(033,102,070,000) ;  ... [f] [F] []
AddKey(034,103,071,000) ;  ... [g] [G] []
AddKey(035,104,072,000) ;  ... [h] [H] []
AddKey(036,106,074,000) ;  ... [j] [J] []
AddKey(037,107,075,000) ;  ... [k] [K] []
AddKey(038,108,076,000) ;  ... [l] [L] []
AddKey(039,246,233,000) ;  ... [ö] [é] []
AddKey(040,228,224,000) ;  ... [ä] [à] []
AddKey(041,167,176,000) ;  ... [§] [°] []
AddKey(043,036,163,000) ;  ... [$] [£] []
AddKey(044,121,089,000) ;  ... [y] [Y] []
AddKey(045,120,088,000) ;  ... [x] [X] []
AddKey(046,099,067,000) ;  ... [c] [C] []
AddKey(047,118,086,000) ;  ... [v] [V] []
AddKey(048,098,066,000) ;  ... [b] [B] []
AddKey(049,110,078,000) ;  ... [n] [N] []
AddKey(050,109,077,000) ;  ... [m] [M] []
AddKey(051,044,059,000) ;  ... [,] [;] []
AddKey(052,046,058,000) ;  ... [.] [:] []
AddKey(053,045,095,000) ;  ... [-] [_] []
AddKey(086,060,062,092) ;  ... [<] [>] [\]

; Tasten auf dem Keypad (15) 
AddKey(055, 042, 000, 000)    ; [*] [ ] [ ] ... 055 = #PB_Key_Multiply 
AddKey(071, 055, 000, 000)    ; [7] [ ] [ ] ... 071 = #PB_Key_Pad7 
AddKey(072, 056, 000, 000)    ; [8] [ ] [ ] ... 072 = #PB_Key_Pad8 
AddKey(073, 057, 000, 000)    ; [9] [ ] [ ] ... 073 = #PB_Key_Pad9 
AddKey(074, 045, 000, 000)    ; [-] [ ] [ ] ... 074 = #PB_Key_Subtract 
AddKey(075, 052, 000, 000)    ; [4] [ ] [ ] ... 075 = #PB_Key_Pad4 
AddKey(076, 053, 000, 000)    ; [5] [ ] [ ] ... 076 = #PB_Key_Pad5 
AddKey(077, 054, 000, 000)    ; [6] [ ] [ ] ... 077 = #PB_Key_Pad6 
AddKey(078, 043, 000, 000)    ; [+] [ ] [ ] ... 078 = #PB_Key_Add 
AddKey(079, 049, 000, 000)    ; [1] [ ] [ ] ... 079 = #PB_Key_Pad1 
AddKey(080, 050, 000, 000)    ; [2] [ ] [ ] ... 080 = #PB_Key_Pad2 
AddKey(081, 051, 000, 000)    ; [3] [ ] [ ] ... 081 = #PB_Key_Pad3 
AddKey(082, 048, 000, 000)    ; [0] [ ] [ ] ... 082 = #PB_Key_Pad0 
AddKey(083, 052, 000, 000)    ; [.] [ ] [ ] ... 083 = #PB_Key_Decimal 
AddKey(181, 047, 000, 000)    ; [/] [ ] [ ] ... 181 = #PB_Key_Divide 

;============================================================== 
; Ende der Include-Datei 
;============================================================== 



; ------------------------- 
; --- Beispielprogramm ---- 
; ------------------------- 

InitSprite() 
InitKeyboard() 
OpenScreen(800,600,16,"Eingabe") 

Repeat 
  
  ExamineKeyboard() 
  
  text$ = KeyboardText(text$) 
  ;text$ + KeyboardInkey() 
  
  ClearScreen(0,0,0) 
  StartDrawing(ScreenOutput()) 
  DrawingMode(1) 
  Locate(10,100) 
  FrontColor(100,255,0) 
  DrawText(text$ + "_") 
  StopDrawing() 
  FlipBuffers() 
  
Until KeyboardPushed(#PB_Key_Escape)
Benutzeravatar
vonTurnundTaxis
Beiträge: 2130
Registriert: 06.10.2004 20:38
Wohnort: Bayreuth
Kontaktdaten:

Beitrag von vonTurnundTaxis »

Also, ehrllich gesagt, fand ich den Vorgänger besser; bei dieser Version kommt es bei schnellem Tippen (im Gegensatz zur alten Version) manchmal dazu, dass zwei Buchstaben unbeabsichtigt groß geschrieben werden.
Nicht durch Zorn, sondern durch Lachen tötet man
ClipGrab | Pastor - jetzt mit kurzen URLs!
Antworten