0

I'm working on a C# project where I need to create the possibility for keyboard input.

A quiz question appears with 3 options. Two players at the same computer have each of their hand at the ASD keys and JKL keys. For the answer options, I'd like for it to be chosen by pressing keyboard keys, as using anything by mouse would be inconvenient for this purpose.

How could I do this? Do I need to use some scripts outside purely C#?

Zaffaro
  • 131
  • 2
  • 12
  • 3
    Is this on web or software app? If web, MVC or WebForms? If software, WF or WPF? You need to give us a lot more info. For all we know, you're making a C#/XAML W8 or W8P application. We need context. We also need your current code to assess the current situation. – Stachu Dec 17 '13 at 13:32

2 Answers2

1

Not enough detail here, please look at: https://stackoverflow.com/questions/how-to-ask

This method retrieves the keys pressed by the user, For example by selecting Ctrl + O, a method called ImportFile() will run. Another useful event to use will be the KeyPressed which is particularly useful firing in textboxes for validation - checking if it's empty.

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    // If user selects Ctrl + O
    if (keyData == (Keys.Control | Keys.O))
    {
        // Call method
        ImportFile();

        return true;
    }
    return base.ProcessCmdKey(ref msg, keyData);
}

Take a look at Key Events: ProcessCmdKey for more details.

Community
  • 1
  • 1
lornasw93
  • 182
  • 4
  • 26
  • http://meta.stackexchange.com/questions/19756/how-do-comments-work. I there's an irony here. But you don't have the rep to comment yet, so fair enough. – Nathan Cooper Dec 17 '13 at 14:31
  • "All users may leave comments on their own posts and any answers given to their own questions. Users with at least 50 reputation may comment on any post. (There is no reputation requirement to post comments on MSO.)" It was an answer ^, not a comment. :) – lornasw93 Dec 17 '13 at 14:41
  • Here's the thing, the first part is an answer and the second part sort of is. A pedant would say links aren't answers, but in all fairness it is a good link. It's good linking other questions but its better to distill what's relevant, or at least it would be if we didn't just answer a not very good vague question. – Nathan Cooper Dec 17 '13 at 14:46
  • Yes, thanks. But this right now is technically a comment, above is technically an answer. Obviously more detail could have been added rather than a relevant link, so I'll give you that. I'll correct it. – lornasw93 Dec 17 '13 at 14:52
1

I assume that we are talking about a web application. Yes you have to use script. Let me explain to you this way: Your c# code is working on the server side. It does not have an effect on client side. Maybe some third party tools like devexpress or something else can be used for this situtaion, but i am not sure these tools can handle key press events. I always prefer developing my own script

$(document).keypress(function(event) { //handle keys });
fealin
  • 443
  • 1
  • 4
  • 13