Register
4coder»Forums»get_next_input not working for me with certain flag in 4.1.6
Jason
181 posts
get_next_input not working for me with certain flag in 4.1.6
6 months, 3 weeks ago Edited by Jason on May 16, 2020, 2:26 p.m. Reason: Initial post
I just downloaded the latest 4coder (4.1.6) and some of my code stopped working. More specifically my command interpreting code for vim like delete and copy operations. I basically just want to gather the first to inputs and store them as a string to then interpret. My code that worked with previous versions of 4coder was:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
    View_ID view = get_active_view(app, Access_Always);
    Scratch_Block scratch(app);
    String_u8 commandString = string_u8_push(scratch, 10);
    
    User_Input firstCommandInput = get_current_input(app);
    User_Input nextCommandInput = get_next_input(app, EventProperty_TextInsert, EventProperty_Escape);
    
    string_append_character(&commandString, *key_code_name[firstCommandInput.event.key.code]);
    
    if (nextCommandInput.abort)
        return;
    else
        string_append(&commandString, nextCommandInput.event.text.string);

    .................


This issue is when I hit the 'get_next_input' function it just hangs no matter what I press. I was using the EventProperty_TextInsert flag because that seemed to be the only flag to get the key I pressed stored as text as well. Is this a bug or I'm I know using the api incorrectly?
Simon Anciaux
919 posts
get_next_input not working for me with certain flag in 4.1.6
6 months, 2 weeks ago
I believe that's a bug, but I'm not familiar with the get_xxx_input API. Try filling a bug report on github.
Jason
181 posts
get_next_input not working for me with certain flag in 4.1.6
6 months, 2 weeks ago
Okay. I did a quick search on the github page and didn't see anything so I posted it as a bug report. We'll see.
Simon Anciaux
919 posts
get_next_input not working for me with certain flag in 4.1.6
6 months, 2 weeks ago
I found out today that if you receive an event that is "InputEventKind_KeyStroke", you can use the field event.key.first_dependent_text to access the text that would be generated by that key. So if you remove the "EventProperty_TextInsert" flag, you might be able to do what you want. Note that there is a comment in the definition of the Input_Event structure that says that "first_dependent_text" is "used internally", so it might not be reliable to use it.

1
2
3
4
5
6
7
User_Input input = get_current_input( app );
    
if ( input.event.kind == InputEventKind_KeyStroke && input.event.key.first_dependent_text ) {
    Input_Event* e = input.event.key.first_dependent_text;
    _assert( e->kind == InputEventKind_TextInsert );
    String_Const_u8 string = e->text.string;
}
Jason
181 posts
get_next_input not working for me with certain flag in 4.1.6
6 months, 2 weeks ago Edited by Jason on May 23, 2020, 7:37 p.m.
Thanks for the suggestion. Though I'm not sure how to make this work with 'get_next_input()'. I can't pass 'InputEventKind_KeyStroke' as a flag to it (function doesn't return unless I abort with escape). I need to use 'get_next_input()' instead of 'get_current_input()' so I can gather my second input for the command string.
Simon Anciaux
919 posts
get_next_input not working for me with certain flag in 4.1.6
6 months, 1 week ago Edited by Simon Anciaux on May 24, 2020, 12:57 p.m.
The flag for get_next_input is EventProperty_AnyKey, not InputEventKind_KeyStroke. The followind example is working for me. There is not default mapping, when you press d it will use get_next_input to insert text in the buffer.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
4coder_default_bidings.cpp - Supplies the default bindings used for default 4coder behavior.
*/

// TOP

#if !defined(FCODER_DEFAULT_BINDINGS_CPP)
#define FCODER_DEFAULT_BINDINGS_CPP

#include "4coder_default_include.cpp"

// NOTE(allen): Users can declare their own managed IDs here.

#if !defined(META_PASS)
#include "generated/managed_id_metadata.cpp"
#endif

CUSTOM_COMMAND_SIG( test ) {
    
    User_Input input = get_current_input( app );
    
    if ( input.event.kind == InputEventKind_KeyStroke && input.event.key.first_dependent_text ) {
        Input_Event* e = input.event.key.first_dependent_text;
        Assert( e->kind == InputEventKind_TextInsert );
        write_text( app, e->text.string );
    }
    
    while ( !input.abort ) {
        
        input = get_next_input( app, EventProperty_AnyKey, EventProperty_Escape );
        
        if ( input.event.kind == InputEventKind_KeyStroke && input.event.key.first_dependent_text ) {
            Input_Event* e = input.event.key.first_dependent_text;
            Assert( e->kind == InputEventKind_TextInsert );
            write_text( app, e->text.string );
        }
    }
}

void
custom_layer_init(Application_Links *app){
    Thread_Context *tctx = get_thread_context(app);
    
    // NOTE(allen): setup for default framework
    default_framework_init(app);
    
    // NOTE(allen): default hooks and command maps
    set_all_default_hooks(app);
    mapping_init(tctx, &framework_mapping);
#if OS_MAC
    // setup_mac_mapping(&framework_mapping, mapid_global, mapid_file, mapid_code);
#else
    // setup_default_mapping(&framework_mapping, mapid_global, mapid_file, mapid_code);
#endif
    
    MappingScope( );
    SelectMapping( &framework_mapping );
    
    SelectMap( mapid_global );
    BindCore( default_startup, CoreCode_Startup );
    BindCore( default_try_exit, CoreCode_TryExit );
    
    SelectMap( mapid_file );
    ParentMap( mapid_global );
    Bind( test, KeyCode_D );
    
    SelectMap( mapid_code );
    ParentMap( mapid_global );
    
}

#endif //FCODER_DEFAULT_BINDINGS

// BOTTOM


Edit: There is a function to retrive text events easier: event_next_text_event in 4coder_events.cpp.
Jason
181 posts
get_next_input not working for me with certain flag in 4.1.6
6 months, 1 week ago
Oh okay, I got it to work. Thanks for looking into it.