Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7087059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:37:37+00:00 2026-05-28T07:37:37+00:00

Dynamic Evaluations in a Key Listener public class KeyUpper { Func<Key, bool> _evaluate; public

  • 0

Dynamic Evaluations in a Key Listener

public class KeyUpper {
    Func<Key, bool> _evaluate;

    public void RegisterEvaluator(Func<Key, bool> evaluate){
        _evaluate = evaluate;
    }

    public void KeyUp(object sender, KeyEventArgs e){
        if (_evaluate(e.KeyCode))
            SomeResponse();
    }

    public void SomeResponse(){
        // ...
    }
}

This Lambda Should Await on Each Line

keyUpper.RegisterEvaluator(key => 
    {
    if (key == Key.A)
        if (key == Key.W)
            if (key == Key.A)
                return true;
    }
);
  • i.e. client code would provide a series of evaluations on the same key argument with the expectation that each line of evaluation would be awaited so that SomeResponse() would be invoked after a sequence of keyup events of 1:A 2:W 3:A
  • obviously at the moment that will never happen because the method is run until the end and key == Key.W will never be true
  • it may not be possible, but is there a way to make the method invocation automagically return from the next line if it evaluates to false but return to it until that line evaluates to true, following that through to the line after that, and to the line after that until the end of the method?
  • i.e. might there be a simple way to provide awaitable lambda expressions of this kind?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T07:37:38+00:00Added an answer on May 28, 2026 at 7:37 am

    If asynchronicity is not a requirement and you are fine with having one thread that almost always waits, you could do it by giving the lambda some blocking way to access the key. For example:

    public void RegisterEvaluator(Func<Func<Key>, bool> evaluate);
    
    …
    
    keyUpper.RegisterEvaluator(
        getKey => getKey() == Key.A && getKey() == Key.W);
    

    RegisterEvaluator would then spin up a new thread that calls the lambda in a loop, passing it a method that accesses the key, blocking if no key is available at the moment, for example using BlockingCollection<Key>.

    If you think doing it this way is wasteful (it is), and you can use async-await, just make the lambda async and change the passed in method to one that is asynchronous too:

    public Task RegisterEvaluator(Func<Func<Task<Key>>, Task<bool>> evaluate);
    
    …
    
    keyUpper.RegisterEvaluator(
        async getKey => await getKey() == Key.A && await getKey() == Key.W);
    

    Implementation of the latter version (using BlockBuffer<Key>) could look like this:

    class KeyUpper
    {
        private readonly BufferBlock<Key> m_keyBuffer = new BufferBlock<Key>();
    
        public async Task RegisterEvaluator(
            Func<Func<Task<Key>>, Task<bool>> evaluate)
        {
            while (true)
            {
                if (await evaluate(m_keyBuffer.ReceiveAsync))
                    SomeResponse();
            }
        }
    
        public void KeyUp(object sender, KeyEventArgs e)
        {
            m_keyBuffer.Post(e.Key);
        }
    
        private void SomeResponse()
        {
            // whatever
        }
    }
    

    Now, I’m not sure how exactly do you want to handle matching the keys, but I think it’s not like this. For example, this sample code would match the sequence of keys BAW, but not AAW.

    Another option would be not using Func<Task<Key>>, but your custom awaitable that can be awaited multiple times and gives one key each time you do that:

    public Task RegisterEvaluator(Func<KeyAwaitable, Task<bool>> evaluate);
    
    …
    
    keyUpper.RegisterEvaluator(
        async getKey => await getKey == Key.A && await getKey == Key.W);
    

    But I think doing it this way is more confusing and harder to do.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can dynamic variables in C# 4.0 be members on a class or passed into
I would like to evaluate a dynamic string value within a switch statement in
dynamic asynchronous iframe creation <!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Transitional//EN http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd> <html xmlns=http://www.w3.org/1999/xhtml>
I'm trying to pass context into a dynamic expression that I evaluate every iteration
Dynamic languages are on the rise and there are plenty of them: e.g. Ruby,
Dynamic allocations with new/delete are said to take place on the free-store , while
Dynamic Data question: I have 2 fields of type Nullable<DateTime> on my model When
Dynamic integer will be any number from 0 to 150. i.e. - number returns
Dynamic Query is a single C# file you add to your project. Anyone know
The dynamic management views of SQL Server 2005 can give usage information about table

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.