Oppure

Loading
06/11/13 16:22
amreo
Ciao

io sto cercando di fare in modo che se io premo un tasto premuto, esegue l'azione, e se è ancora premuto, la tPressed viene aumentata, e se supera 360, fa l'azione e ritorna a 0.
io non riesco a capire perchè viene sempre Rinizializzata, ritornando il valore di inizio(0).
io per provare ho provato a inizializzarla con 30.
la funzione ScriviDati() scrive le info della struttura.

Tipo progetto: Window Console

Imports System.Windows.Forms

Module Module1

    Sub Main()
        InitInput()
        Do
            Application.DoEvents()
            GetInput()
            ScriviDati()
        Loop
    End Sub

    Sub ScriviDati()
        For Each i As ManagerKeyInfo In keyM
            Console.WriteLine("Azione: " & i.ActionName & "Premuto: " & i.Pressed & "TPreseed: " & i.tPressed)
        Next
    End Sub


    Dim keyM As New List(Of ManagerKeyInfo)

    Public Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Integer) As Short
    Public Function GetKeyState(ByVal key1 As Integer) As Boolean
        Dim s As Short
        s = GetAsyncKeyState(key1)
        If s = 0 Then Return False
        Return True
    End Function

    'Ottiene l'elenco di tutti gli Input
    Public Sub GetInput()
        'inserire il codice relativo ai tasti dell' input usare GetKeyState per i tasti
        'controlla ogni tasto necessario
        For Each key As ManagerKeyInfo In keyM
            If key.UpdatePress() = True Then DoActionDisponibili(key)
        Next

    End Sub

    'Esegui l'azione dei tasti
    Sub DoActionDisponibili(MKI As ManagerKeyInfo)
        Select Case MKI.ActionName
            Case ReceiveActionsName.Null Or ReceiveActionsName.NonImpostato
                'niente
            Case ReceiveActionsName.Up
            Case ReceiveActionsName.Down
            Case ReceiveActionsName.Left
            Case ReceiveActionsName.Right

            Case Else
                MsgBox("errore")
        End Select
    End Sub

    'Inizializza gli input
    Sub InitInput()
        'imposta KP/Tick
        ManagerKeyInfo.tPTick = 20
        'Aggiunge i tasti a keyM con ADD
        '
        keyM.Add(New ManagerKeyInfo(False, ReceiveActionsName.Up, Keys.Up, 30))
        keyM.Add(New ManagerKeyInfo(False, ReceiveActionsName.Down, Keys.Down, 30))
        keyM.Add(New ManagerKeyInfo(False, ReceiveActionsName.Left, Keys.Left, 30))
        keyM.Add(New ManagerKeyInfo(False, ReceiveActionsName.Right, Keys.Right, 30))
    End Sub

End Module


Public Structure ManagerKeyInfo

    'indica se il tasto è premuto
    Friend Pressed As Boolean

    'indica quanto tempo è stato premuto: se supera 360 rifa l'azione di premere
    Friend tPressed As Short
    Friend Const MaxPressed As Short = 360
    Friend Shared tPTick As Short = 10 'tempo di premitura su un Tick

    'Indica il testo premuto
    Friend KeyName As Keys

    'Nome dell' azione che il tasto deve eseguire
    Friend ActionName As ReceiveActionsName


    'Costruttori
    Friend Sub New(ByVal tPressed As Short, ByVal ActionName As ReceiveActionsName, ByVal Key As Keys, Optional ByVal Pressed As Boolean = False)
        Me.tPressed = tPressed
        Me.Pressed = Pressed
        Me.ActionName = ActionName
        Me.KeyName = Key
    End Sub
    Friend Sub New(ByVal Pressed As Boolean, ByVal ActionName As ReceiveActionsName, ByVal Key As Keys, Optional ByVal tPressed As Short = 0)
        Me.tPressed = tPressed
        Me.Pressed = Pressed
        Me.ActionName = ActionName
        Me.KeyName = Key
    End Sub

    'Settaggio tPress(restituisce true se deve eseguire l'azione)
    Function UpdatePress() As Boolean
        ' Dim PrsChange As Boolean = MainForm.GetKeyState(KeyName)
        Dim PrsChange As Boolean = True 'tecnicamente la riga corretta è quella sopra, ma per prova uso questa
        Dim EseguiAzione As Boolean = False
        If PrsChange = False Then
            If Pressed = True Then
                Pressed = False
                tPressed = 0
            End If
        ElseIf PrsChange = True Then
            If Pressed = True Then
                Beep()
                tPressed += tPTick
                If tPressed >= MaxPressed Then
                    tPressed = 0
                    EseguiAzione = True
                End If
            ElseIf Pressed = False Then
                Pressed = True
                tPressed += tPTick
                EseguiAzione = True
            End If
        End If
        Return EseguiAzione
    End Function

End Structure

Friend Enum ReceiveActionsName As SByte
    NonImpostato = -1
    Null = 0
    Up = 1
    Down = 2
    Right = 3
    Left = 4
    'altre azioni
End Enum
Ultima modifica effettuata da amreo 06/11/13 16:30
aaa
06/11/13 16:40
Cosa è

Keys

?
06/11/13 16:45
amreo
un enum di System.Window.Form

contiene l'elenco di tutti i tasti disponibili sulla tastiera(anche mouse)
aaa
06/11/13 17:00
Ma quindi il tipo di applicazione deve essere modificata in Win Form ...
06/11/13 17:16
amreo
no, ho aggiunto il riferimento window.form
aaa
06/11/13 17:23
Ah ... ecco ...

Comunque ... cosa dovrebbe succedere ? Quali tasti provare ?
06/11/13 17:41
amreo
solo i tasti inizializza in InitInput. le freccette: attenzione decommenta la prima linea di UpdatePress e commenta la seconda
aaa
08/11/13 19:16
amreo
Risolto. funziona come deve funzionare(non ho capito il motivo pero funziona cosi.

ho creato un' altra structure: contiene i risultati di UpdatePress di ManagerKeyInfo
Public Structure MKICheckResult

    Public Property ActionRun As Boolean

    Public Property MKI As ManagerKeyInfo

    Public Sub New(ActionRun As Boolean, MKI As ManagerKeyInfo)
        Me.ActionRun = ActionRun
        Me.MKI = MKI
    End Sub
End Structure


ho modificato il tipo della funzione UpdatePress di ManagerKeyInfo da Boolean in MKICheckResult.

ho modificato GetInput: ho dichiarato una variabile
Dim MKICR As MKICheckResult

ho sostituito il ciclo for each in ciclo for
For i = 0 To KeyM.Count
MKICR = KeyM(i).UpdatePress()
 keyM(i) = New ManagerKeyInfo(MKICR.MKI.Pressed, MKICR.MKI.ActionName, MKICR.MKI.KeyName, MKICR.MKI.tPressed)
Next


l'errore non ho capito qual'è ma credo che sia dovuto alla lista, che non cambia i valori degli elementi, quindi ho dovuto usare il costruttore.:)



Ultima modifica effettuata da amreo 08/11/13 19:17
aaa