Skip to content

Commit

Permalink
Added config file
Browse files Browse the repository at this point in the history
  • Loading branch information
KoB-Kirito committed Jun 14, 2020
1 parent 8a6958b commit c3d4b56
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 25 deletions.
4 changes: 4 additions & 0 deletions CountOnKeypress.vbproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
</ItemGroup>

</Project>
98 changes: 73 additions & 25 deletions Program.vb
Original file line number Diff line number Diff line change
@@ -1,25 +1,52 @@
Imports System.IO
Imports System.Runtime.InteropServices
Imports System.Text
Imports Newtonsoft.Json

Module Program
Public Const COUNT_UP_KEY = ConsoleKey.PageUp
Public Const COUNT_DOWN_KEY = ConsoleKey.PageDown
Public Const RESET_KEY = ConsoleKey.Delete

Sub Main(args As String())
Console.SetWindowSize(17, 4)
Console.CursorVisible = False

'Parse json
If Not File.Exists("config.json") Then
Dim tCfg = New Config With {
.ShowConsoleOutput = True,
.CountUpKey = ConsoleKey.PageUp,
.CountDownKey = ConsoleKey.PageDown,
.ResetKey = ConsoleKey.Delete,
.CounterDefaultValue = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
}
Dim tJson = JsonConvert.SerializeObject(tCfg, Formatting.Indented)
File.WriteAllText("config.json", tJson, New UTF8Encoding(False))
End If
Dim json = File.ReadAllText("config.json", New UTF8Encoding(False))
Dim cfg = JsonConvert.DeserializeObject(Of Config)(json)

'Parse settings
Dim COUNT_UP_KEY = cfg.CountUpKey
Dim COUNT_UP_KEY_2 = cfg.CountUpKey2
Dim COUNT_DOWN_KEY = cfg.CountDownKey
Dim COUNT_DOWN_KEY_2 = cfg.CountDownKey2
Dim RESET_KEY = cfg.ResetKey
Dim RESET_KEY_2 = cfg.ResetKey2

Dim defaultValue(9) As Integer
Dim consoleOutput As Boolean

If args.Length > 0 Then
Boolean.TryParse(args(0), consoleOutput)
Else
consoleOutput = cfg.ShowConsoleOutput
End If

Dim counter(9) As Integer
For i = 0 To 9
If args.Length > i + 1 Then
Integer.TryParse(args(i + 1), defaultValue(i))
Else
defaultValue(i) = cfg.CounterDefaultValue(i)
End If
counter(i) = defaultValue(i)
If consoleOutput AndAlso defaultValue(i) <> 0 Then
Expand All @@ -28,32 +55,40 @@ Module Program
End If
Next

Dim cKey() = {ConsoleKey.NumPad0,
ConsoleKey.NumPad1,
ConsoleKey.NumPad2,
ConsoleKey.NumPad3,
ConsoleKey.NumPad4,
ConsoleKey.NumPad5,
ConsoleKey.NumPad6,
ConsoleKey.NumPad7,
ConsoleKey.NumPad8,
ConsoleKey.NumPad9}

Dim cKey() As Integer = {ConsoleKey.NumPad0,
ConsoleKey.NumPad1,
ConsoleKey.NumPad2,
ConsoleKey.NumPad3,
ConsoleKey.NumPad4,
ConsoleKey.NumPad5,
ConsoleKey.NumPad6,
ConsoleKey.NumPad7,
ConsoleKey.NumPad8,
ConsoleKey.NumPad9}

Dim cState(9) As Short
'Check keypresses forever
Do

'Fixes bug of states not resetting
For i = 0 To 9
cState(i) = GetAsyncKeyState(cKey(i))
Next

'Count up
If GetAsyncKeyState(COUNT_UP_KEY) Then
If GetAsyncKeyState(COUNT_UP_KEY) AndAlso (COUNT_UP_KEY_2 = 0 OrElse GetAsyncKeyState(COUNT_UP_KEY_2)) Then
For i = 0 To 9
If GetAsyncKeyState(cKey(i)) Then
If cState(i) Then
counter(i) += 1
IO.File.WriteAllText($"counter{i}.txt", counter(i))
If Not consoleOutput Then
If consoleOutput Then
SetCursorPosition(i)
Console.Write($"{counter(i),4}")
End If

' Wait to only trigger once per keypress
While GetAsyncKeyState(COUNT_UP_KEY) AndAlso GetAsyncKeyState(cKey(i))
Threading.Thread.Sleep(15)
End While

Exit For
Expand All @@ -62,18 +97,19 @@ Module Program
End If

'Count down
If GetAsyncKeyState(COUNT_DOWN_KEY) Then
If GetAsyncKeyState(COUNT_DOWN_KEY) AndAlso (COUNT_DOWN_KEY_2 = 0 OrElse GetAsyncKeyState(COUNT_DOWN_KEY_2)) Then
For i = 0 To 9
If GetAsyncKeyState(cKey(i)) Then
If cState(i) Then
counter(i) -= 1
IO.File.WriteAllText($"counter{i}.txt", counter(i))
If Not consoleOutput Then
If consoleOutput Then
SetCursorPosition(i)
Console.Write($"{counter(i),4}")
End If

' Wait to only trigger once per keypress
While GetAsyncKeyState(COUNT_DOWN_KEY) AndAlso GetAsyncKeyState(cKey(i))
Threading.Thread.Sleep(15)
End While

Exit For
Expand All @@ -82,18 +118,19 @@ Module Program
End If

'Reset
If GetAsyncKeyState(RESET_KEY) Then
If GetAsyncKeyState(RESET_KEY) AndAlso (RESET_KEY_2 = 0 OrElse GetAsyncKeyState(RESET_KEY_2)) Then
For i = 0 To 9
If GetAsyncKeyState(cKey(i)) Then
If cState(i) Then
counter(i) = defaultValue(i)
IO.File.WriteAllText($"counter{i}.txt", counter(i))
If Not consoleOutput Then
If consoleOutput Then
SetCursorPosition(i)
Console.Write($"{counter(i),4}")
End If

' Wait to only trigger once per keypress
While GetAsyncKeyState(RESET_KEY) AndAlso GetAsyncKeyState(cKey(i))
Threading.Thread.Sleep(15)
End While

Exit For
Expand Down Expand Up @@ -145,7 +182,18 @@ Module Program
End Sub

<DllImport("user32.dll")>
Public Function GetAsyncKeyState(ByVal vKey As ConsoleKey) As Short
Public Function GetAsyncKeyState(ByVal vKey As Integer) As Short
End Function

End Module
End Module

Public Structure Config
Public ShowConsoleOutput As Boolean
Public CountUpKey As Integer
Public CountUpKey2 As Integer
Public CountDownKey As Integer
Public CountDownKey2 As Integer
Public ResetKey As Integer
Public ResetKey2 As Integer
Public CounterDefaultValue As Integer()
End Structure

0 comments on commit c3d4b56

Please sign in to comment.