Kirjautuminen

Haku

Tehtävät

Keskustelu: Ohjelmointikysymykset: VB6: Näppäimistön luvusta vielä

upsi [07.10.2007 20:19:34]

#

Löysin nyt Ohje-valikosta tällaisenkin tavan lukea näppäimistöä, mutta se on pitkän ja vaikean näköinen. Pystyisikö joku lyhentämään (turhat pois ja itse asia näkyville ilman lukuisia kummallisia muuttujia, varmastikin mahdollista... Itse karsin vääriä juttuja kuitenkin.)?

' This example demonstrates the Console.ReadKey() method
Imports System
Imports System.Text
Imports Microsoft.VisualBasic

Class Sample
   Public Shared Sub Main()
      Dim cki As ConsoleKeyInfo
      '                   0        1         2         3         4         5         6
      '                   123456789012345678901234567890123456879012345678901234567890
      Dim m1 As String = "This example discovers the console and modifier keys " & _
                         "that you press." & vbCrLf
      Dim m2 As String = "Press any combination of CTL, ALT, and SHIFT modifier keys, " & _
                         "and a console key." & vbCrLf & _
                         "Press the Escape (Esc) key to quit: "
      Dim m3 As String = "You pressed "
      Dim m4 As String = " (character '{0}')."
      Dim sb As New StringBuilder()
      '
      ' The Console.TreatControlCAsInput property prevents this example from
      ' ending if you press CTL+C, however all other operating system keys and
      ' shortcuts, such as ALT+TAB or the Windows Logo key, are still in effect.
      '
      Console.TreatControlCAsInput = True
      Console.WriteLine(m1)
      Do
         Console.WriteLine(m2)
         sb.Length = 0
         cki = Console.ReadKey(True)
         sb.Append(m3)
         If cki.Modifiers <> 0 Then
            If(cki.Modifiers And ConsoleModifiers.Alt) <> 0 Then
               sb.Append("ALT+")
            End If
            If(cki.Modifiers And ConsoleModifiers.Shift) <> 0 Then
               sb.Append("SHIFT+")
            End If
            If(cki.Modifiers And ConsoleModifiers.Control) <> 0 Then
               sb.Append("CTL+")
            End If
         End If
         sb.Append(cki.Key.ToString())
         sb.AppendFormat(m4, cki.KeyChar)
         sb.AppendLine().AppendLine()
         Console.WriteLine(sb.ToString())
      Loop While cki.Key <> ConsoleKey.Escape
   End Sub 'Main ' Note: This example requires the Escape (Esc) key.
End Class 'Sample
'
'This example produces results similar to following text:
'
'This example discovers the console and modifier keys that you press.
'
'Press any combination of CTL, ALT, and SHIFT modifier keys, and a console key.
'Press the Escape (Esc) key to quit:
'You pressed A (character 'a').
'
'
'Press any combination of CTL, ALT, and SHIFT modifier keys, and a console key.
'Press the Escape (Esc) key to quit:
'You pressed SHIFT+A (character 'A').
'
'
'Press any combination of CTL, ALT, and SHIFT modifier keys, and a console key.
'Press the Escape (Esc) key to quit:
'You pressed ALT+SHIFT+CTL+A (character ' ').
'
'
'Press any combination of CTL, ALT, and SHIFT modifier keys, and a console key.
'Press the Escape (Esc) key to quit:
'You pressed Escape (character '?').
'

Vastaus

Aihe on jo aika vanha, joten et voi enää vastata siihen.

Tietoa sivustosta