diff --git a/src/internal.cpp b/src/internal.cpp index 614e42c..510e56b 100644 --- a/src/internal.cpp +++ b/src/internal.cpp @@ -74,7 +74,7 @@ namespace vkvm { auto buttonPressed(KeyCode keyCode) -> void { lockSharedMemory(); auto reg = getRegisters(); - if (reg->keyboardBuffer_index_write == sizeof(reg->keyboardBuffer)) { + if (reg->keyboardBuffer_index_write >= sizeof(reg->keyboardBuffer)) { reg->keyboardBuffer_index_write = 0; } reg->keyboardBuffer[reg->keyboardBuffer_index_write++] = keyCode; diff --git a/src/vkvm.cpp b/src/vkvm.cpp index 46e5709..fdbff7a 100644 --- a/src/vkvm.cpp +++ b/src/vkvm.cpp @@ -210,8 +210,17 @@ namespace vkvm { } auto getLastPressedKey() -> KeyCode { - //TODO(julian): get key properly - return KeyCode(0); + lockSharedMemory(); + auto reg = getRegisters(); + if(reg->keyboardBuffer_index_read == reg->keyboardBuffer_index_write){ + return KeyCode(0); + } + KeyCode keyCode = (KeyCode)reg->keyboardBuffer[reg->keyboardBuffer_index_read++]; + if(reg->keyboardBuffer_index_read >= sizeof(reg->keyboardBuffer)){ + reg->keyboardBuffer_index_read = 0; + } + unlockSharedMemory(); + return keyCode; } } \ No newline at end of file