Click here to Skip to main content
15,860,943 members
Home / Discussions / C#
   

C#

 
QuestionType Casting from OleDb Value Types Returns Invalid Cast Exception Pin
User9874325-Dec-17 23:09
professionalUser9874325-Dec-17 23:09 
AnswerRe: Type Casting from OleDb Value Types Returns Invalid Cast Exception Pin
Gerry Schmitz26-Dec-17 12:28
mveGerry Schmitz26-Dec-17 12:28 
GeneralRe: Type Casting from OleDb Value Types Returns Invalid Cast Exception Pin
User9874326-Dec-17 12:52
professionalUser9874326-Dec-17 12:52 
GeneralRe: Type Casting from OleDb Value Types Returns Invalid Cast Exception Pin
User9874326-Dec-17 13:02
professionalUser9874326-Dec-17 13:02 
QuestionAwait/Async Question Pin
Kevin Marois24-Dec-17 10:51
professionalKevin Marois24-Dec-17 10:51 
AnswerRe: Await/Async Question Pin
Dave Kreskowiak24-Dec-17 15:15
mveDave Kreskowiak24-Dec-17 15:15 
GeneralRe: Await/Async Question Pin
Kevin Marois24-Dec-17 15:22
professionalKevin Marois24-Dec-17 15:22 
QuestionGetting a list of Key pressed Pin
Kenneth Haugland22-Dec-17 9:52
mvaKenneth Haugland22-Dec-17 9:52 
I need to know the key that is currently pressed in a C# WPF application, so:
List<Key> PressedKeys = new List<Key>();

private void Window_KeyDown(object sender, KeyEventArgs e)
{
    //If key is pressed this stop resending
    if (!PressedKeys.Contains(e.Key))
    {
        PressedKeys.Add(e.Key);
        txtKeysDown.Text = String.Join(", ", PressedKeys);
    }

}

private void Window_KeyUp(object sender, KeyEventArgs e)
{
    PressedKeys.Remove(e.Key);
    txtKeysDown.Text = String.Join(", ", PressedKeys);
}
This works, sort of. However, it does not give me the raw input from the keyboard so when AltGr is pressed it canges from Key.RightAlt => Key.LeftCtrl + Key.RightAlt whitch is Windows interpretation according to this: AltGr key - Wikipedia[^]. I dont want this, as I want the user to be able to "bind" any key to a spesific action.

So I tried to use the user32.dll call:
[DllImport("user32.dll", EntryPoint = "GetKeyboardState", SetLastError = true)]
private static extern bool NativeGetKeyboardState([Out] byte[] keyStates);

        private static bool GetKeyboardState(byte[] keyStates)
        {
            if (keyStates == null)
                throw new ArgumentNullException("keyState");
            if (keyStates.Length != 256)
                throw new ArgumentException("The buffer must be 256 bytes long.", "keyState");
            return NativeGetKeyboardState(keyStates);
        }

        private static byte[] GetKeyboardState()
        {
            byte[] keyStates = new byte[256];
            if (!GetKeyboardState(keyStates))
                throw new Win32Exception(Marshal.GetLastWin32Error());
            return keyStates;
        }

        private static bool AnyKeyPressed()
        {
            byte[] keyState = GetKeyboardState();
            // skip the mouse buttons
            return keyState.Skip(8).Any(state => (state & 0x80) != 0);
        }
    }


With the function:
bool MyIsKeyDown(Key key)
{

    return (((byte)GetKeyboardState()[(int)key] & 0x80) & 0x80)>0;
}


and code implementation:
private void Window_KeyDown(object sender, KeyEventArgs e)
{

    foreach (Key item in Enum.GetValues(typeof(Key)))
    {
        if (MyIsKeyDown(item))
        {
            if (!PressedKeys.Contains(e.Key))
            {
                PressedKeys.Add(e.Key);
                txtKeysDown.Text = String.Join(", ", PressedKeys);
            }
        }
    }

    ////If key is pressed this stop resending
    //if (!PressedKeys.Contains(e.Key))
    //{
    //    PressedKeys.Add(e.Key);
    //    txtKeysDown.Text = String.Join(", ", PressedKeys);
    //}

}


But still, windows insisted on giving me the replacement Key.RightAlt => Key.LeftCtrl + Key.RightAlt. So how do I get the key pressed on the actual keyboard?
AnswerRe: Getting a list of Key pressed Pin
Kenneth Haugland23-Dec-17 5:10
mvaKenneth Haugland23-Dec-17 5:10 
AnswerRe: Getting a list of Key pressed Pin
Gerry Schmitz23-Dec-17 5:58
mveGerry Schmitz23-Dec-17 5:58 
GeneralRe: Getting a list of Key pressed Pin
Kenneth Haugland23-Dec-17 10:14
mvaKenneth Haugland23-Dec-17 10:14 
GeneralRe: Getting a list of Key pressed Pin
Gerry Schmitz23-Dec-17 18:38
mveGerry Schmitz23-Dec-17 18:38 
SuggestionMessage Closed Pin
21-Dec-17 23:20
professionalmatthewproctor21-Dec-17 23:20 
GeneralRe: Looking for feedback for new .Net / C# error tracking and reporting tool Pin
OriginalGriff22-Dec-17 0:57
mveOriginalGriff22-Dec-17 0:57 
GeneralRe: Looking for feedback for new .Net / C# error tracking and reporting tool Pin
Eddy Vluggen22-Dec-17 4:08
professionalEddy Vluggen22-Dec-17 4:08 
QuestionPopulate graph from datagridview Pin
Member 1125947820-Dec-17 22:51
Member 1125947820-Dec-17 22:51 
GeneralRe: Populate graph from datagridview Pin
Ralf Meier21-Dec-17 2:50
professionalRalf Meier21-Dec-17 2:50 
GeneralRe: Populate graph from datagridview Pin
Member 1125947821-Dec-17 2:54
Member 1125947821-Dec-17 2:54 
AnswerRe: Populate graph from datagridview Pin
Gerry Schmitz21-Dec-17 10:58
mveGerry Schmitz21-Dec-17 10:58 
QuestionHow can I make a Location-like collapsible properties? Pin
Member 1358759420-Dec-17 18:52
Member 1358759420-Dec-17 18:52 
QuestionRe: How can I make a Location-like collapsible properties? Pin
Richard MacCutchan20-Dec-17 21:36
mveRichard MacCutchan20-Dec-17 21:36 
AnswerRe: How can I make a Location-like collapsible properties? Pin
Ralf Meier21-Dec-17 2:42
professionalRalf Meier21-Dec-17 2:42 
AnswerRe: How can I make a Location-like collapsible properties? Pin
Gerry Schmitz21-Dec-17 10:53
mveGerry Schmitz21-Dec-17 10:53 
QuestionAccess a combobox defined in datatemplate Pin
Hervend20-Dec-17 1:18
Hervend20-Dec-17 1:18 
AnswerRe: Access a combobox defined in datatemplate Pin
Gerry Schmitz20-Dec-17 4:15
mveGerry Schmitz20-Dec-17 4:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.