I wanted to know if someone can log my keystrokes if i am on a virtual machine. Is it more secure to use a computer or a virtual machine on that computer.
RedGrittyBrick is right.
Here's how it would work:
A hardware keylogger will obviously capture anything typed on the keyboard it is attached to. So that includes keystrokes that are forwarded to a VM.
I would expect a software keylogger running on the host system should also capture anything typed on the local keyboard before it gets to any VM
A software keylogger running in a VM would capture keystrokes from the virtual keyboard presented by the host, so it would not capture keystrokes consumed by the host nor keystrokes forwarded to other VMs.
Keyloggers do not capture keystrokes generated remotely and transmitted to the computer hosting the keylogger by network protocols similar to SSH.
Whether host-end keyloggers can capture keystrokes from remote-desktop clients (e.g. RDP) presumably depends on details of the implementation of the remote desktop service and of the keylogger.